Here, I will save basic coredata information for anyone/me would like a quick refresh for these fundamentals from time to time.
- Core Data is not a database, but instead it manages an Object Graph.
-
NSManagedObject
andNSManagedObjectContext
in Core Data are not thread-safe. -
Core Data avoids data race issues by operating on
NSManagedObject
instances andNSManagedObjectContext
instances in a serial queue.
This is why we need to place the operation code within the closures of perform or performAndWait. -
Developers should perform operations on the main thread queue for the view context and the managed object instances registered within it. Similarly, we should perform operations on the serial queue created by the private context for private contexts and the managed objects registered within them.
- Enable Core Data Concurrency Debugging Parameters
To void issues like .......... to be continued
ModelContext
of SwiftUI is like a wrapped version ofNSManagedObjectContext
References:
-
Core Data Fundamentals - Exploring the Core Data Stack
https://cocoacasts.com/exploring-the-core-data-stack -
Concurrent Programming in SwiftData https://betterprogramming.pub/concurrent-programming-in-swiftdata-c9bf021a4c2d
How Does Core Data Works
The heart of Core Data is the Persistent Store Coordinator.
Persistent Store Coordinator is instantiated first when core data is created.
Persistent Store Coordinator needs Managed Object Model instance to know what data schema for the application look like.
After that, the Managed Object Context is initialized.
Now the core data is setup, and application is ready to interact the application Persistent Store through the Managed Object Context.
When the changes made to Managed Context Object or saved, it pushes them to the Persistent Store Coordinator which send the changes to the corresponding Persistent Store.
When you have multiple Persistent Store, the Persistent Store Coordinator figures out which one is needed to save the changes.