当前位置:网站首页>Coredata storage to do list
Coredata storage to do list
2022-07-25 09:53:00 【yueliangmua】
Check... When creating the project CoreData, The system will automatically AppDelegate The generated code , And generate a xcdatamodeld file , It doesn't matter if you don't check it , New one checked CoreData Project , Copy the generated code and create it yourself xcdatamodeld file .
stay xcdatamodeld New in the file ENTITIES Replace the original Todo Structure , And add two Attribute Element and select type , You can set the default value on the right function panel , The bottom layer of the system will generate a class.
stay AppDelegate You can see the generated persistence container persistentContainer and SaveContext Method is used to judge whether the data has changed and store the data .
Open again through the sandbox address Application Support The file shows sqlite Database files , open Todos.sqlite file ( Suggest using DB Browser for SQLite App open )

You can see the database structure and saved data
Instantiate an empty container , Add a to-do and save
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext// obtain appDelegat Object to get the contents of the container
let todo = Todo(context: context)// Instantiate an empty container
todo.name = name
todos.append(todo)
(UIApplication.shared.delegate as! AppDelegate).saveContext()// Judge whether the data is changed and save
tableView.insertRows(at: [IndexPath(row: todos.count - 1, section: 0)], with: .automatic)Delete the to-do and save
First delete the local data , Then delete the memory data , Because the local is deleted through memory , For example, data [1,2,3] If you delete the memory first 1 Then the memory will become [2,3] And the local memory is now based on the 1 individual
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
// Delete the row from the data source
context.delete(todos[indexPath.row])// First delete local , Delete memory again , Found locally through memory
todos.remove(at: indexPath.row)
appDelegate.saveContext()
// tableView.deleteRows(at: [indexPath], with: .fade)
//saveData()
tableView.reloadData()
} else if editingStyle == .insert {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}Fixed usage fetches data
if let todos = try? context.fetch(Todo.fetchRequest()){
self.todos = todos
}else{
print(" from SQLite Failed to get data from inside ")
}// Fixed usage fetches data because context Not only in the new to-do list , So it can be proposed as a global variable
Modify and delete the to-do list, call the function to save
appDelegate.saveContext()Sort
The attributes stored in the database are unordered after sorting , So you need to be in ENTITY Add an attribute representing the sequence number ( Database migration )

todos[indexPath.row].orderID = Int16(indexPath.row)
appDelegate.saveContext()Assign sorting rules when retrieving data
let request = Todo.fetchRequest()
request.sortDescriptors = [NSSortDescriptor(key: kOrderID, ascending: true)]
if let todos = try? context.fetch(Todo.fetchRequest()){
self.todos = todos
}else{
print(" from SQLite Failed to get data from inside ")
}// Fixed usage fetches data
边栏推荐
- Hyperautomation for the enhancement of automation in industries 论文翻译
- MLX90640 红外热成像传感器测温模块开发笔记(三)
- TensorFlow raw_rnn - 实现seq2seq模式中将上一时刻的输出作为下一时刻的输入
- CDA Level1多选题精选
- Kotlin collaboration: foundation and use of collaboration
- [deep learning] convolutional neural network
- 初识Opencv4.X----图像直方图匹配
- MLX90640 红外热成像仪测温模块开发说明
- Evolution based on packnet -- review of depth estimation articles of Toyota Research Institute (TRI) (Part 1)
- 【降维打击】希尔伯特曲线
猜你喜欢

TensorFlow raw_rnn - 实现seq2seq模式中将上一时刻的输出作为下一时刻的输入

深度估计自监督模型monodepth2论文总结和源码分析【理论部分】

*7-1 CCF 2015-09-1 sequence segmentation

Preliminary understanding and implementation of wechat applet bottom navigation bar

Development history of convolutional neural network (part)

多通道振弦、温度、模拟传感信号采集仪数据查看和参数修改

CDA Level1知识点总结之业务分析报告与数据可视化报表

CCF 201509-3 模板生成系统

数据分析业务核心

Mixed supervision for surface defect detection: from weakly to fully supervised learning
随机推荐
[data mining] nearest neighbor and Bayesian classifier
初识Opencv4.X----图像直方图均衡
CUDA 解释 - 深度学习为何使用 GPU
Verdi 基础介绍
括号匹配问题
Mixed supervision for surface-defect detection: from weakly to fully supervised learning:表面缺陷检测的混合监督
Evolution based on packnet -- review of depth estimation articles of Toyota Research Institute (TRI) (Part 2)
单目深度估计模型Featdepth实战中的问题和拓展
CCF 201509-3 模板生成系统
MLX90640 红外热成像传感器测温模块开发笔记(二)
数字IC设计SOC入门进阶
yolov5实现小数据集的目标检测--kolektor缺陷数据集
Wechat applet realizes the rotation map (automatic switching & manual switching)
Principle analysis of self supervised depth estimation of fish eye image and interpretation of omnidet core code
[deep learning] convolutional neural network
FLASH read / write operation and flash upload file of esp8266
First knowledge of opencv4.x --- image histogram drawing
TensorFlow2 安装快速避坑汇总
CDA Level1多选题精选
初识Opencv4.X----图像卷积