CoreData could not fulfill a fault for

Hmm. Are you properly implementing concurrency following this guide? The problem you are seeing is a common one when using core data across multiple threads. Object was deleted in your “background context”, while then it’s being accessed by another context. Calling [context processPendingChanges] on your background context after the delete but before the save may … Read more

How do I copy or move an NSManagedObject from one context to another?

First, having more than one NSManagedObjectContext on a single thread is not a standard configuration. 99% of the time you only need one context and that will solve this situation for you. Why do you feel you need more than one NSManagedObjectContext? Update That is actually one of the few use cases that I have … Read more

How can I track/observe all changes within a subgraph?

You will want to listen for the NSManagedObjectContextObjectsDidChangeNotification to pick up all changes to your data model. This can be done using code like the following: [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleDataModelChange:) name:NSManagedObjectContextObjectsDidChangeNotification object:myManagedObjectContext]; which will trigger -handleDataModelChange: on any changes to the myManagedObjectContext context. Your -handleModelDataChange: method would look something like this: – (void)handleDataModelChange:(NSNotification *)note { … Read more

What is NSManagedObjectContext’s performBlock: used for?

The methods performBlock: and performBlockAndWait: are used to send messages to your NSManagedObjectContext instance if the MOC was initialized using NSPrivateQueueConcurrencyType or NSMainQueueConcurrencyType. If you do anything with one of these context types, such as setting the persistent store or saving changes, you do it in a block. performBlock: will add the block to the … Read more

Multiple NSEntityDescriptions Claim NSManagedObject Subclass

Post-automatic-caching This should not happen anymore with NSPersistent[CloudKit]Container(name: String), since it seems to cache the model automatically now (Swift 5.1, Xcode11, iOS13/MacOS10.15). Pre-automatic-caching NSPersistentContainer/NSPersistentCloudKitContainer does have two constructors: init(name: String) init(name: String, managedObjectModel model: NSManagedObjectModel) The first is just a convenience initializer calling the second with a model loaded from disk. The trouble is that … Read more

Core Data background context best practice

This is an extremely confusing topic for people approaching Core Data for the first time. I don’t say this lightly, but with experience, I am confident in saying the Apple documentation is somewhat misleading on this matter (it is in fact consistent if you read it very carefully, but they don’t adequately illustrate why merging … Read more