当前位置:网站首页>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
边栏推荐
- Wechat applet realizes the rotation map (automatic switching & manual switching)
- 单目深度估计模型Featdepth实战中的问题和拓展
- ARM预备知识
- 【数据挖掘】第三章 数据分析基础
- Evolution based on packnet -- review of depth estimation articles of Toyota Research Institute (TRI) (Part 2)
- Defect detection network -- hybrid supervision (kolektor defect data set reproduction)
- 【深度学习模型部署】使用TensorFlow Serving + Tornado部署深度学习模型
- 基于PackNet的演进——丰田研究院(TRI)深度估计文章盘点(上)
- [dimension reduction strike] Hilbert curve
- 【深度学习】自编码器
猜你喜欢

手持振弦VH501TC采集仪传感器的连接与数据读取

BSP3 电力监控仪(功率监控仪)端子定义和接线

C语言基础

T5论文总结

First acquaintance with opencv4.x --- ROI interception

ARM预备知识

鱼眼图像自监督深度估计原理分析和Omnidet核心代码解读

【深度学习】自编码器

First knowledge of opencv4.x ---- mean filtering

Customize dialog to realize the pop-up box of privacy clause statement imitating Netease cloud music
随机推荐
Android & kotlin: puzzle solution
ISP图像信号处理
[dimension reduction strike] Hilbert curve
TensorFlow raw_rnn - 实现seq2seq模式中将上一时刻的输出作为下一时刻的输入
【降维打击】希尔伯特曲线
ARM GIC简介
Matlab drawing | some common settings of axis
单目深度估计自监督模型Featdepth解读(上)——论文理解和核心源码分析
CCF 201512-4 送货
CCF 201503-3 节日
CCF 201503-4 网络延时
无向连通图邻接表的创建输出广度深度遍历
从Anaconda到TensorFlow到Jupyter一路踩坑一路填平
How to add other PHP versions to MAMP
Sort out personal technology selection in 2022
@3-2 optimal threshold of CCF 2020-12-2 final forecast
【数据挖掘】第四章 分类任务(决策树)
【Tensorflow2安装】Tensorflow2.3-CPU安装避坑指南!!!
CUDA 解释 - 深度学习为何使用 GPU
[data mining] Chapter 2 understanding data