Core Data lightweight migration: change relationship from To-One to To-Many

隐身守侯 提交于 2019-12-13 00:43:20

问题


I recently made a few changes to my Core Data model file and I'm trying to perform a lightweight migration. I've already worked with lightweight migrations before so I know I did all the setup correctly.

My only problem is that I changed the type of a relationship: it used to be To-One and now it's To-Many. According to Apple's documentation, lightweight migrations should work just fine in this case, but when I run the code and the system starts migrating the data, there is a crash.

I noticed in the stack trace the following method is causing the crash:

3   CoreData  0x008df2d1 -[_NSSQLEntityMigrationDescription 
        _populateSQLValuesForDestinationToOne:fromSourceToOne:] + 145

So for some reason the method being called is trying to migrate the relationship as if it still was a To-One relationship.

Why is the mapping model being inferred incorrectly, and how can I fix it?

Thanks!

EDIT

This is the error message that I get:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException',
reason: '*** setObjectForKey: object cannot be nil (key: Z8CUSCONNECTION)'

cusConnection is the name of the relationship that I changed from To-One to To-Many.

Also, here is the stack trace:

0   CoreFoundation                      0x01588746 __exceptionPreprocess + 182
1   libobjc.A.dylib                     0x01211a97 objc_exception_throw + 44
2   CoreFoundation                      0x01479c9c -[__NSDictionaryM setObject:forKey:] + 940
3   CoreData                            0x009182d1 -[_NSSQLEntityMigrationDescription _populateSQLValuesForDestinationToOne:fromSourceToOne:] + 145
4   CoreData                            0x00918d30 -[_NSSQLEntityMigrationDescription _populateSQLValuesForVirtualToOnesWithMigrationContext:] + 1648
5   CoreData                            0x00918202 -[_NSSQLEntityMigrationDescription _generateSQLValueMappingsWithMigrationContext:] + 962
6   CoreData                            0x0091998b -[_NSSQLEntityMigrationDescription sqlValueForColumnName:migrationContext:] + 59
7   CoreData                            0x00923cf1 -[_NSSQLTableMigrationDescription createInsertStatementForEntityMigration:migrationContext:] + 689
8   CoreData                            0x00922d07 -[_NSSQLTableMigrationDescription appendStatementsToPerformMigration:migrationContext:] + 1143
9   CoreData                            0x0091d024 -[_NSSQLiteStoreMigrator createEntityMigrationStatements] + 900
10  CoreData                            0x0091b088 -[_NSSQLiteStoreMigrator performMigration:] + 104
11  CoreData                            0x0092753a -[NSSQLiteInPlaceMigrationManager migrateStoreFromURL:type:options:withMappingModel:toDestinationURL:destinationType:destinationOptions:error:] + 1962
12  CoreData                            0x008b9104 -[NSMigrationManager migrateStoreFromURL:type:options:withMappingModel:toDestinationURL:destinationType:destinationOptions:error:] + 692
13  CoreData                            0x00908605 -[NSStoreMigrationPolicy(InternalMethods) migrateStoreAtURL:toURL:storeType:options:withManager:error:] + 341
14  CoreData                            0x009074b5 -[NSStoreMigrationPolicy migrateStoreAtURL:withManager:metadata:options:error:] + 117
15  CoreData                            0x00909380 -[NSStoreMigrationPolicy(InternalMethods) _gatherDataAndPerformMigration:] + 3136
16  CoreData                            0x008c2c76 __91-[NSPersistentStoreCoordinator addPersistentStoreWithType:configuration:URL:options:error:]_block_invoke + 5270
17  CoreData                            0x008d12ff gutsOfBlockToNSPersistentStoreCoordinatorPerform + 191
18  libdispatch.dylib                   0x03593bef _dispatch_client_callout + 14
19  libdispatch.dylib                   0x03576b0d _dispatch_barrier_sync_f_invoke + 144
20  libdispatch.dylib                   0x0357623f dispatch_barrier_sync_f + 105
21  CoreData                            0x008c03f7 _perform + 183
22  CoreData                            0x007b272c -[NSPersistentStoreCoordinator addPersistentStoreWithType:configuration:URL:options:error:] + 236
23  Nimbus II                           0x00117fd2 -[AppDelegate persistentStoreCoordinator] + 674
24  Nimbus II                           0x002c79b6 +[UserInformationParser saveData:] + 374
25  Nimbus II                           0x001c2f2e __47-[SynchronizationController synchronizeData]_block_invoke + 366
26  libdispatch.dylib                   0x035715ea _dispatch_call_block_and_release + 15
27  libdispatch.dylib                   0x03593bef _dispatch_client_callout + 14
28  libdispatch.dylib                   0x0357b1ef _dispatch_root_queue_drain + 1092
29  libdispatch.dylib                   0x0357cb70 _dispatch_worker_thread3 + 115
30  libsystem_pthread.dylib             0x038d843e _pthread_wqthread + 1050
31  libsystem_pthread.dylib             0x038d5f72 start_wqthread + 34

回答1:


Lightweight (or Automatic) migration does support changing relationship from To-one to To-many.

Several steps:

  • Create new model (xcdatamodel)
  • Reset the type if you've created a class (from a class type to NSSet since its one-to-many relationship)
  • Change the relationship for the entity in the new model you just created (from one-to-one to one-to-many)
  • In the Data Model Inspector of the relationship, under the Versioning, set the Renaming ID with the last relationship name (without s).

The lightweight migrator will create mapping based on it.

It works as same as renaming the entity, etc.



来源:https://stackoverflow.com/questions/34749301/core-data-lightweight-migration-change-relationship-from-to-one-to-to-many

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!