I gave a talk on Core Data in a Nutshell and its concurrency rules.
Core Data Fundamentals- @ajithrnayak
View Slide
Core Data Vs Developers
Core Data StackNSPersistentStoreCoordinatorSQLiteNSPersistentContainerNSManagedObjectContextNSPersistentStoreNSManagedObjectModelApp
NSManagedObjectContext• Like a scratchpad• fetch, change, add and delete• Create more than one contexts• Updates persistent store
Concurrency policiesConcurrencyTypeNSPrivateQueueConcurrencyType (background queue)NSMainQueueConcurrencyType (application’s main queue)
perform(_:)• asynchronous method• gets wrapped in a autorelease pool.performAndWait(_:)• synchronous method• will block the calling thread• does not wrap the block in an autorelease pool.
Summary• A managed object context is tied to a serial queue.• Create only two contexts for simplicity.• Concurrency types are two but contexts can be many.• Context executes on a “private” queue.• Performance heavy operations should be performed on the background queue.• Don’t pass NSManagedObject instances between queues.• To avoid unnecessary saves, be aware of automatically triggered broadcasts.
Thank you