Small knowledge, big challenge! This paper is participating in theEssentials for programmers”Creative activities
1. Create Core Data
Once created, we should see a similar page.
Add Entity, Add a body to which you can Add properties of the response and dependencies between the body. We can also think of this as a class.
2. Set classes for managing and persisting application objects
Create NSPersistentContainer in the AppDelegate
lazy var persistentContainer: NSPersistentContainer = {let container = NSPersistentContainer(name: "Model") / / local persistence resource loading container. LoadPersistentStores {description, error in if let error = error { fatalError("Unable to load persistent stores: \(error)") } } return container }() // MARK: - Core Data Saving support func saveContext () { let context = persistentContainer.viewContext if context.hasChanges { do { try context.save() } catch { let nserror = error as NSError fatalError("Unresolved error \(nserror), \(nserror.userInfo)") } } }Copy the code
3. Add data
The main thing we use is insertNewObject
Static func createStudent(name: String, sid: String, age: Int, block: () - > Void) {/ / obtain the data management context object let app = UIApplication. Shared. The delegate as! AppDelegate let context = app. PersistentContainer. ViewContext / / insert data let stu = NSEntityDescription.insertNewObject(forEntityName: "Student", into: context) as! Student let teacher = NSEntityDescription.insertNewObject(forEntityName: "Teacher", into: context) as! Sid = sid stu. Age = Int16(age) stu. TeachBy = Int16(age) stu Teacher if context.hasChanges {do {// save data try context.save() print("Insert new stu(\(name)) successful.") block()} catch { print(error) } } }Copy the code
4. Query data
The data is queried using NSFetchRequest
Static func queryStudentWithName(_ name: String) {/ / obtain the data management context object let app = UIApplication. Shared. The delegate as! AppDelegate let context = app. PersistentContainer. ViewContext / / query data let fetchStudents = NSFetchRequest<Student>. Init (entityName: "Student") // Conditional query fetchStudents. Predicate = NSPredicate(format: "name = \"\(name)\"") do { let students = try context.fetch(fetchStudents) print("students count = \(students.count)") for student in students { print("student name = \(student.name!) ") } print("query student:\(name) successful.") } catch { } }Copy the code
5. Modify data
The process is mainly read → modify → save
Static func updateStudentWithName(_ name: String, newName:) String) {/ / obtain the data management context object let app = UIApplication. Shared. The delegate as! AppDelegate let context = app. PersistentContainer. ViewContext / / query data let fetchStudents = NSFetchRequest<Student>. Init (entityName: "Student") // Conditional query fetchStudents. Predicate = NSPredicate(format: "name = \"\(name)\"") do { let students = try context.fetch(fetchStudents) if ! Students. IsEmpty {students[0].name = newName if context.haschanges {do {// Save data try context.save() print("Update new" Student successful.") } catch { print(error) } } } } catch { } }Copy the code
6. Delete data
The process is mainly read – delete – save.
Static func deleteStudentWithName(_ name: String) {/ / obtain the data management context object let app = UIApplication. Shared. The delegate as! AppDelegate let context = app. PersistentContainer. ViewContext / / query data let fetchStudents = NSFetchRequest<Student>. Init (entityName: "Student") // Conditional query fetchStudents. Predicate = NSPredicate(format: "name = \"\(name)\"") do { let students = try context.fetch(fetchStudents) if ! Delete (Students [0]) if context.haschanges {do {// Save data try context.save() print("delete \(name) successful.") } catch { print(error) } } } } catch { } }Copy the code
GitHub source: github.com/wenweijia/C…