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:
- This is the 33th migration (I have already done this 32 times without any problem and not by adding a single attribute...)
- 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.
- The crash happens after every possible change to the model (add attribute, rename attribute, add/remove entity)
- I use XCode 12.2 & Swift 5.3. The same crash happens in XCode 12.1 as well
- The crash insists if I try a heavyweight migration with a custom mapping model, as well.
- 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.
container.loadPersistentStores(completionHandler: { (description, error) in}. That's the strange thing. - nick3389Show in Finderon your.xcdatamodeldfile inside the Xcode project. It will take you to Finder where multiple versions of your data model file will be present in the.xcdatamodelformat. Copy/paste the current and last version into some place else for inspecting. Right click on.xcdatamodelfile and hitShow Package Contents- it will open upcontentsfile 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 TyagilastSavedToolsVersion="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