How to use Core Data

Issue #785 Core Data Responding to changes in a managed object context Calling mergeChanges on a managed object context will automatically refresh any managed objects that have changed. This ensures that your context always contains all the latest information. Note that you don鈥檛 have to call mergeChanges on a viewContext when you set its automaticallyMergesChangesFromParent property to true. In that case, Core Data will handle the merge on your behalf....

February 26, 2021 路 6 min 路 Khoa Pham

How to convert struct to Core Data NSManagedObject

Issue #706 Use Mirror and set key value as NSManagedObject subclasses from NSObject import CoreData final class ManagedObjectConverter { func convert<M>(m: M, context: NSManagedObjectContext) throws -> NSManagedObject { let entityName = String(describing: m) guard let entityDescription = NSEntityDescription.entity( forEntityName: entityName, in: context ) else { throw AppError.parsing } let managedObject = NSManagedObject( entity: entityDescription, insertInto: context ) let mirror = Mirror(reflecting: m) guard mirror.displayStyle == .struct else { throw AppError.parsing } for case let (label?...

December 7, 2020 路 1 min 路 Khoa Pham

How to use CoreData safely

Issue #686 I now use Core Data more often now. Here is how I usually use it, for example in Push Hero From iOS 10 and macOS 10.12, NSPersistentContainer that simplifies Core Data setup quite a lot. I usually use 1 NSPersistentContainer and its viewContext together with newBackgroundContext attached to that NSPersistentContainer In Core Data, each context has a queue, except for viewContext using the DispatchQueue.main, and each NSManagedObject retrieved from 1 context is supposed to use within that context queue only, except for objectId property....

October 25, 2020 路 2 min 路 Khoa Pham

How to force FetchRequest update in SwiftUI

Issue #623 Listen to context changes notification and change SwiftUI View state let changes = [NSDeletedObjectsKey: ids] NSManagedObjectContext.mergeChanges( fromRemoteContextSave: changes, into: [context] ) try context.save() struct ListView: View { @Environment(\.managedObjectContext) var context private var didSave = NotificationCenter.default.publisher(for: .NSManagedObjectContextDidSave) @State private var refreshing: Bool = false var body: some View { makeContent() .onReceive(didSave) { _ in self.refreshing.toggle() } } } We need to actually use that State variable for it to have effect...

March 16, 2020 路 1 min 路 Khoa Pham

How to batch delete in Core Data

Issue #622 Read Implementing Batch Deletes If the entities that are being deleted are not loaded into memory, there is no need to update your application after the NSBatchDeleteRequest has been executed. However, if you are deleting objects in the persistence layer and those entities are also in memory, it is important that you notify the application that the objects in memory are stale and need to be refreshed....

March 15, 2020 路 1 min 路 Khoa Pham