How to use NSFetchedResultsController memory wise in Core Data

Issue #982 If you’re using NSFetchedResultsController in Core Data, it might take up a lot of memory, especially when working with large datasets. To keep your app running smoothly, it’s important to manage memory efficiently Use Fetch Limits and Batch Fetching If your dataset is large, limit the number of objects fetched at a time. You can achieve this by setting a fetchBatchSize and a fetchLimit on your NSFetchRequest. fetchBatchSize: This controls how many objects Core Data will fetch in memory at a time....

September 24, 2024 · 3 min · 547 words · Khoa

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’t 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 · 1109 words · Khoa

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 · 91 words · Khoa

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 · 299 words · Khoa

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 · 81 words · Khoa

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. To do this, first make sure the resultType of the NSBatchDeleteRequest is set to NSBatchDeleteRequestResultType....

March 15, 2020 · 1 min · 191 words · Khoa