Core Data Multiple NSEntityDescriptions claim the NSManagedObject subclass

Hello everyone,

I'm trying to adopt the new Staged Migrations for Core Data and I keep running into an error that I haven't been able to resolve.

The error messages are as follows:

warning: Multiple NSEntityDescriptions claim the NSManagedObject subclass 'Movie' so +entity is unable to disambiguate.
warning:  	 'Movie' (0x60000350d6b0) from NSManagedObjectModel (0x60000213a8a0) claims 'Movie'.
error: +[Movie entity] Failed to find a unique match for an NSEntityDescription to a managed object subclass

This happens for all of my entities when they are added/fetched. Movie is an abstract entity subclass, and it has the error error: +[Movie entity] Failed to find which is unique to the subclass entities, but this occurs for all entities.

The NSPersistentContainer is loaded only once, and I set the following option after it's loaded:

storeDescription.setOption(
    [stages],
    forKey: NSPersistentStoreStagedMigrationManagerOptionKey
)

The warnings and errors only appear after I fetch or save to context. It happens regardless of whether the database was migrated or not. In my test project, using the generic NSManagedObject with NSEntityDescription.insertNewObject(forEntityName: "MyEntity", into: context) does not cause the issue. However, using the generic NSManagedObject is not a viable option for my app.

Setting the module to "Current Project Module" doesn't change anything, except that it now prints "claims 'MyModule.Show'" in the warnings. I have verified that there are no other entities with the same name or renameIdentifier.

Has anyone else encountered this issue, or can offer any suggestions on how to resolve it?

Thanks in advance for any help!

I've made a mistake while writing here and replaced my function that returns the NSStagedMigrationManager with only [stages]. It should have been NSStagedMigrationManager(stages). This is only an issue in the post and not in my code.

Would you mind to provide a minimal project with detailed steps to reproduce the issue? I'll be intersted in taking a look. Your post can contain a link to where your test project is hosted.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

I've created a sample project on GitHub that demonstrates exactly what's happening with the Staged Migrations warning: Sample project

Thanks for sharing the project, which does reproduce the issue.

It seems to me that the issue is triggered because NSStagedMigrationManager and NSPersistentContainer load the same models into separate Core Data stacks, which leads to a conflict.

Avoiding using APIs that call +entity under the hood eliminates the warnings. For example, using insertNewObject to create the new object, as shown below, fixes the issue in your demo app:

//let entity = MyEntity(context: context)
let entity = NSEntityDescription.insertNewObject(forEntityName: "MyEntity", into: context) as! MyEntity
entity.name = name
try context.save()

However, this doesn't reflect the best practices of using strong types, nor is it practical for a real-world project because there are other Core Data APIs using +entity under the hood.

I'd hence suggest that you file a feedback report for the Core Data folks to take a look – If you do so, please share your report ID here for folks to track.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

@DTS Engineer I've filed feedback detailing this issue, including links to both this thr ead and the sample project.

FB18334791

Core Data Multiple NSEntityDescriptions claim the NSManagedObject subclass
 
 
Q