Mac Assigning NSManagedObject to NSPersistentStore

Hello,

I have a iOS app I was looking at porting to Mac.

I'm having an issue with both the Mac (Designed for iPad) and Mac Catalyst Destinations. I can't test Mac due to too many build issues.

I'm trying to assign a new NSManagedObject into a NSPersistentStore.

let object = MyObject(context: context)
context.assign(object, to: nsPersistentStore)

This works fine for iOS/iOS Simulator/iPhone/iPad. But on the Mac it's crashing with

FAULT: NSInvalidArgumentException: Can't assign an object to a store that does not contain the object's entity.; {
Thread 1: "Can't assign an object to a store that does not contain the object's entity."

The error indicates that current model version of the specified persistent store (nsPersistentStore) doesn't contain the entity of the object (MyObject). My best guess is that your Mac version is using a wrong model version – If that is not the case, I'd be interested in taking a closer look in case you can provide a minimal project that reproduces the error.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

It appears to be related explicitly to persistent store. Removing the assign call allows the object to be created, modified, saved as expected.

Further, I tried to editing objects with the following code

 let managedObjectContext = managedObjectContext.child(with: .mainQueueConcurrencyType)
                    let object = managedObjectContext.object(with: objectID) as! Object

And this crashed with

Thread 1: "Object 0x82018e630c8dd320 <x-coredata://E8D86530-2F0D-4680-98AC-D42B1FF23DE4/Object/p33> persistent store is not reachable from this NSManagedObjectContext's coordinator"

Again, this isn't an issue for iOS, but it does appear to be an issue for both Mac and VisionOS.

It's also not feasible to provide a minimal project that is minimal.

Yeah, I think you are right that it is related to the store, which is tied to a Core Data model. To confirm if your store has the right model, you can use NSPersistentContainer to load the store, use managedObjectModel to dump the model, and check if it has the right entity for MyObject.

assign(_:to:) is used extensively in the case where one Core Data stack is used to manage multiple stores, such as this Apple sample. I will be super curious why if it doesn't work on your side if the store has the right model.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

My best guess right now is that something with the managedObjectContext or my CoreDataStack in the environment behaves differently on Mac than it does on iOS and VisionOS.

I have 3 stores and the version hash in the metadata is up to date for all of them. So I don't think it's related to the managedObjectModel.

Mac Assigning NSManagedObject to NSPersistentStore
 
 
Q