0
votes

I am trying to make a lightweight migration in Core Data. I just to want to add a new attribute in an existing Entity. New model version is created for this purpose. Every time the app runs, it crashes at runtime immediately after launch with this message:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Can't use fetch request with fetched property description (entity model mismatch).'

Also, I have enabled SQL debug flag and before the crash I get this:

CoreData: annotation: Disconnecting from sqlite database.

Notes:

  1. This is the 33th migration (I have already done this 32 times without any problem and not by adding a single attribute...)
  2. I use the latest API by loading the persistent store through the coordinator but the same happens if I do it the old way by adding the persistent store.
  3. The crash happens after every possible change to the model (add attribute, rename attribute, add/remove entity)
  4. I use XCode 12.2 & Swift 5.3. The same crash happens in XCode 12.1 as well
  5. The crash insists if I try a heavyweight migration with a custom mapping model, as well.
  6. I use FirebaseCrashlytics and I managed to take a report for this crash:
Fatal Exception: NSInvalidArgumentException
0  CoreFoundation                 0x1aca24878 __exceptionPreprocess
1  libobjc.A.dylib                0x1c0f7ac50 objc_exception_throw
2  CoreFoundation                 0x1ac91d4a4 -[NSCache init] <- CRASH HERE
3  CoreData                       0x1b2c4a984 -[NSFetchedPropertyDescription setFetchRequest:]
4  CoreData                       0x1b2c4aab4 -[NSFetchedPropertyDescription _createCachesAndOptimizeState]
5  CoreData                       0x1b2c3d608 -[NSEntityDescription(_NSInternalMethods) _createCachesAndOptimizeState]
6  CoreData                       0x1b2c8305c -[NSManagedObjectModel(_NSInternalMethods) _createCachesAndOptimizeState]
7  CoreData                       0x1b2c83f94 -[NSManagedObjectModel(_NSInternalMethods) _setIsEditable:optimizationStyle:]
8  CoreData                       0x1b2b30e40 -[NSStoreMigrationPolicy(InternalMethods) _gatherDataAndPerformMigration:]
9  CoreData                       0x1b2c9e494 __91-[NSPersistentStoreCoordinator addPersistentStoreWithType:configuration:URL:options:error:]_block_invoke
10 CoreData                       0x1b2cab6f4 gutsOfBlockToNSPersistentStoreCoordinatorPerform
11 libdispatch.dylib              0x1ac618db0 _dispatch_client_callout
12 libdispatch.dylib              0x1ac627428 _dispatch_lane_barrier_sync_invoke_and_complete
13 CoreData                       0x1b2c9b268 _perform
14 CoreData                       0x1b2b30744 -[NSPersistentStoreCoordinator addPersistentStoreWithType:configuration:URL:options:error:]
15 CoreData                       0x1b2c9d3f8 -[NSPersistentStoreCoordinator _doAddPersistentStoreWithDescription:privateCopy:completeOnMainThread:withHandler:]
16 libdispatch.dylib              0x1ac61724c _dispatch_call_block_and_release
17 libdispatch.dylib              0x1ac618db0 _dispatch_client_callout
18 libdispatch.dylib              0x1ac61bddc _dispatch_queue_override_invoke
19 libdispatch.dylib              0x1ac62993c _dispatch_root_queue_drain
20 libdispatch.dylib              0x1ac62a120 _dispatch_worker_thread2
21 libsystem_pthread.dylib        0x1f5e3c7c8 _pthread_wqthread
22 libsystem_pthread.dylib        0x1f5e4375c start_wqthread

Any help is much appreciated, as I cannot update my application in this state.

1
The error message seems to say that you added a fetched property description, not a regular attribute. Is that correct? Can you edit your question to include the fetch request that's causing this? - Tom Harrington
No fetch request is made because I can't even reach to that point. The crash happens in container.loadPersistentStores(completionHandler: { (description, error) in}. That's the strange thing. - nick3389
OK, how about whether you added a fetched property description? - Tom Harrington
Hit Show in Finder on your .xcdatamodeld file inside the Xcode project. It will take you to Finder where multiple versions of your data model file will be present in the .xcdatamodel format. Copy/paste the current and last version into some place else for inspecting. Right click on .xcdatamodel file and hit Show Package Contents - it will open up contents file which can be opened up in any text editor - it's xml code. Copy the xml code from both current / previous and compare the diff using something like - diffchecker.com/diff. You might find something you forgot about - Tarun Tyagi
Just did that. The only diff is at the start of the file for these tags: lastSavedToolsVersion="17511" systemVersion="20B29". The last model has this: lastSavedToolsVersion="16119" systemVersion="19G73". Also another change is the new attribute which seems normal and a record for the entity I added the attribute which changed from this: <element name="entity_name" positionX="-549" positionY="-9" width="128" height="330"/> to this: <element name="entity_name" positionX="-549" positionY="-9" width="128" height="329"/>. Everything seems fine to me, correct? - nick3389

1 Answers

1
votes

It seems that there was a "bad" model version in my data model. As I explained in my initial post I have 33 model versions. After a technical question I raised in Apple Code Level Support, an engineer guided me to delete one by one the past versions as he discovered that one of them is the responsible for the crash after keeping only the last two. Eventually, he was correct as I found that model version 15 was the culprit. The issue is most probably an internal bug of Core data and I have opened a Feedback ticket as well. Let's hope this will be resolved in future releases of XCode. For now, I deleted all the past versions - culprit included - as I don't need them anymore and the crash has disappeared.