核心数据轻量级迁移:将关系从一对多更改为一对多 [英] Core Data lightweight migration: change relationship from To-One to To-Many

查看:59
本文介绍了核心数据轻量级迁移:将关系从一对多更改为一对多的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我对核心数据模型文件做了一些更改,并尝试执行轻量级迁移。以前我已经进行过轻量级迁移,所以我知道我正确地完成了所有设置。



我唯一的问题是我更改了关系类型:成为一对一,现在是一对多。根据Apple的文档,在这种情况下,轻量级迁移应该可以正常工作,但是当我运行代码并且系统开始迁移数据时,会发生崩溃。



我在堆栈跟踪中注意到以下方法导致崩溃:

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

So由于某种原因,被调用的方法试图迁移该关系,就好像它仍然是一对一关系一样。



为什么无法正确推断映射模型,我该如何解决?



谢谢! p>

编辑



这是我收到的错误消息:

  ***由于未捕获的异常 NSInvalidArgumentException而终止应用程序,
原因: *** setObjectForKey:对象不能为零(键:Z8CUSCONNECTION)

cusConnection 是我从中更改的关系的名称一对多。



此外,这是堆栈跟踪:

  0 CoreFoundation 0x01588746 __exceptionPreprocess + 182 
1 libobjc.A.dylib 0x01211a97 objc_exception_throw + 44
2 CoreFoundation 0x01479c9c-[__ NSDictionaryM setObject:forKey:] + 940
3 CoreData 0x009182 [_NSSQLEntityMigrationDescription _populateSQLValuesForDestinationToOne:fromSourceToOne:] + 1 45
4 CoreData 0x00918d30-[_ NSSQLEntityMigrationDescription _populateSQLValuesForVirtualToOnesWithMigrationContext:] + 1648
5 CoreData 0x00918202-[_ NSSQLEntityMigrationDescription _generateSQLValueMappingsWithMigrationContext:] + 962
6ForState:ForValueToColumnsForMigrationContext:] +962
6ForDataToTo $ b 7 CoreData 0x00923cf1-[_ NSSQLTableMigrationDescription createInsertStatementForEntityMigration:migrationContext:] + 689
8 CoreData 0x00922d07-[_ NSSQLTableMigrationDescription appendStatementsToPerformMigration:migrationContext:] + 1143
9M数据0x0091d实体10 CoreData 0x0091b088-[_ NSSQLiteStoreMigrator performMigration:] + 104
11 CoreData 0x0092753a-[NSSQLiteInPlaceMigrationManager migrationStoreFromURL:type:options:withMappingModel:toDestinationURL:destinationType:destinationOptions:error:] + 1962
12 CoreData 0x008b9104-[NSMigrationManager migrationStoreFromFromURL:type:options:withMappingModel:toDestinationURL:] + 692
13 CoreData 0x00908605-[NSStoreMigrationPolicy(InternalMethods)migrationStoreAtURL:toURL:storeType:options:withManager:error:] + 341
14 CoreData 0x009074b5-[NSStoreMigrationPolicy migrateStoreAtURL:withManager:元数据:选项:错误: ] + 117
15 CoreData 0x00909380-[NSStoreMigrationPolicy(InternalMethods)_gatherDataAndPerformMigration:] + 3136
16 CoreData 0x008c2c76 __91- [NSPersistentStoreCoordinator addPersistentStoreWithType:configuration:URL:options:error $ b:] 17℃ oreData 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-[AppDelegatepersistentStoreCoordinator] + 674
24 Nimbus II 0x002c79bser + User saveData:] + 374
25 Nimbus II 0x001c2f2e __47- [SynchronizationController syncnizeData] _block_invoke + 366
26 libdispatch.dylib 0x035715ea _dispatch_call_block_and_re租赁+ 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


解决方案

轻量级(或自动)迁移确实支持将关系从一对一更改为一对多。



几个步骤:




  • 创建新模型( xcdatamodel

  • 如果已创建类(自一对一关系以来,从类类型更改为 NSSet

  • 在新视图中更改实体的关系您刚创建的模型(从一对一到一对多)

  • 在relati的数据模型检查器中onship,在 Versioning (版本控制)下,将 Renaming ID(重命名ID)设置为最近的关系名称(不包含 s )。



轻量级迁移器将基于它创建映射。



它的工作原理与重命名实体等。


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

解决方案

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.

这篇关于核心数据轻量级迁移:将关系从一对多更改为一对多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆