当前位置:网站首页>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
边栏推荐
- CCF 201503-4 网络延时
- Save pdf2image PDF file as JPG nodejs implementation
- First knowledge of opencv4.x ---- mean filtering
- 初识Opencv4.X----为图像添加高斯噪声
- Kotlin collaboration: foundation and use of collaboration
- Creation of adjacency table of undirected connected graph output breadth depth traversal
- CUDA 解释 - 深度学习为何使用 GPU
- matlab如何导入大量数据
- 深入理解pytorch分布式并行处理工具DDP——从工程实战中的bug说起
- ISP图像信号处理
猜你喜欢
随机推荐
SD/SDIO/EMMC
A number converted from a decimal integer to another base
关于MLOps中的数据工程,你一定要知道的.......
深度估计自监督模型monodepth2论文总结和源码分析【理论部分】
AI模型风险评估 第1部分:动机
MinkowskiEngine 安装
ARMV8 datasheet学习
ADC简介
CUDA explanation - why GPU is used in deep learning
Chmod and chown invalidate the files of the mounted partition
[deep learning] convolutional neural network
[data mining] nearest neighbor and Bayesian classifier
手持振弦VH501TC采集仪传感器的连接与数据读取
Segmentation-based deep-learning approach for surface-defectdetection(基于分割的表面缺陷深度学习检测方法)
Learning new technology language process
MLOps专栏介绍
First knowledge of opencv4.x ---- mean filtering
How to add other PHP versions to MAMP
UI prototype resources
Principle analysis of self supervised depth estimation of fish eye image and interpretation of omnidet core code








