Previews Crash on CoreData model using 'Parent Entity'

I have a CoreData model with two entities, 'User' and 'Player', that both use 'Person' as their 'Parent Entity'. While the App appears to work correctly in the simulator, including with CloudKit via NSPersistentCloudKitContainer, I get a crash in Xcode Previews:

libswiftCore.dylib [
  AGScoringModel/Persistence.swift:183: Fatal error: #init(inMemory:): Failed to load persistent stores:Error Domain=NSCocoaErrorDomain Code=134110 "An error occurred during persistent store migration." UserInfo={sourceURL=file:///Users/ebg/Library/Developer/.../CoreDataStores/private/database.sqlite, reason=Cannot migrate store in-place: Cannot merge multiple root entity source tables into one destination entity root table, destinationURL=file:///Users/ebg/Library/Developer/.../CoreDataStores/private/database.sqlite, NSUnderlyingError=0x600000ce02a0 {Error Domain=NSCocoaErrorDomain Code=134110 "An error occurred during persistent store migration." UserInfo={message=Cannot merge multiple root entity source tables into one destination entity root table, destinationRootEntity=Person, NSUnderlyingException=Cannot merge multiple root entity source tables into one destination entity root table, sourceRootEntities=(
    User,
    Player
  ), reason=Cannot merge multiple root entity source tables into one destination entity root table}}}
]

Why is this? Something in my configuration for persistent container?

  • Maybe just a 'Preview Cache' issue; cache has 'no parent entity' (original code); can't transition to 'parent entity' (refactored code).

Add a Comment

Accepted Reply

Based on the error alone it seems that you have an old database schema stored on disk, which cannot be migrated with an automatic migration to your current schema (there are many reasons the lightweight/automatic schema migration might fail, I'll link to some documentation below). You can delete the simulators that Previews uses by running the following command: xcrun simctl --set previews delete all. That should clear any persistent data causing problems in your previews.

You might also consider setting up your persistent store to run in memory when you are using previews. This will help you to avoid such issues in the future.

You can read more about migrations in the documentation:

Lightweight Migrations

Staged Migrations

Manual Migrations

  • My Persistence Controller has an 'inMemory' option. How can it be invoked for a Preview?

  • @GoZoner You would need to somehow initialize a different controller for your previews, and in that case use the inMemory option. How you do this would depend largely on how your app is structured, so it is hard to speculate how you would achieve this.

  • Thanks. Yes, every #Preview creates a controller (to pass as an @EnvironmentObject or from which to derive the NSManagedObjectContext) - clearly create a #Preview-specific controller.

Add a Comment

Replies

Based on the error alone it seems that you have an old database schema stored on disk, which cannot be migrated with an automatic migration to your current schema (there are many reasons the lightweight/automatic schema migration might fail, I'll link to some documentation below). You can delete the simulators that Previews uses by running the following command: xcrun simctl --set previews delete all. That should clear any persistent data causing problems in your previews.

You might also consider setting up your persistent store to run in memory when you are using previews. This will help you to avoid such issues in the future.

You can read more about migrations in the documentation:

Lightweight Migrations

Staged Migrations

Manual Migrations

  • My Persistence Controller has an 'inMemory' option. How can it be invoked for a Preview?

  • @GoZoner You would need to somehow initialize a different controller for your previews, and in that case use the inMemory option. How you do this would depend largely on how your app is structured, so it is hard to speculate how you would achieve this.

  • Thanks. Yes, every #Preview creates a controller (to pass as an @EnvironmentObject or from which to derive the NSManagedObjectContext) - clearly create a #Preview-specific controller.

Add a Comment