我已经以一些简单的方式(删除属性、添加属性、删除索引)更新了现有 iPhone 应用程序的模型,并且可以使用自动轻量级迁移来迁移持久存储。
由于数据集的典型大小,处理时间并非微不足道,并且需要为用户提供反馈。
NSMigrationManager
提供了一个简单但有用的migrationProgress
值,用于在执行迁移时发送 KVO 通知。这构成了提供反馈的基础,但是尝试使用推断模型 ( [NSMappingModel inferredMappingModelForSourceModel:destinationModel:error:]
) 会导致完全相同的数据集的时间完全不同。
原始 iPhone (2G) 上的配置文件结果,缓存大小:磁盘上的 1.785 MB。
自动推断轻量级迁移
PROFILE: CacheManager -migrateStore
PROFILE: 0.6130 (+0.6130) models loaded
PROFILE: 1.1759 (+0.5629) delegate -CacheManagerWillMigrate:
PROFILE: 1.2516 (+0.0757) persistent store coordinator loaded
PROFILE: 5.1436 (+3.8920) automatic lightweight migration completed
PROFILE: 5.5435 (+0.3999) delegate -CacheManagerDidFinishMigration:withError:
手动推断迁移
PROFILE: CacheManager -migrateStore
PROFILE: 0.6660 (+0.6660) models loaded
PROFILE: 1.1471 (+0.4811) inferred mapping model generated
PROFILE: 1.4046 (+0.2574) delegate -CacheManagerWillMigrate:
PROFILE: 1.5058 (+0.1013) persistent store coordinator loaded
PROFILE: 22.6952 (+21.1894) manual migration completed
PROFILE: 23.1478 (+0.4525) delegate -CacheManagerDidFinishMigration:withError:
因此,使用推断模型,手动迁移的时间是自动迁移的 5 倍以上!
更新:模型加载
“迁移选项”的核心数据文档说:NSPersistentStoreCoordinator
NSInferMappingModelAutomaticallyOption
...如果找不到映射模型,协调器将尝试推断映射模型。
这就是为什么 XCode 构建、编译和捆绑的映射模型必须被移除(或只是非目标)以允许推断和轻量级迁移发生。
这是一个很大的不一致之处,而且轻量级选项NSPersistentStoreCoordinator -addPersistentStoreWithType:configuration:URL:options:error:
在处理时绝对不提供进度指示。
migrationProgress
任何人都可以提供一种在自动迁移期间获取值的受支持方式,或者一种将推断映射模型配置为在手动处理期间与自动处理一样快的方式吗?
更新:错误报告
与 WWDC 的工程师交谈,他们要求提供错误报告,要求migrationProgress
进行自动轻量级迁移处理。
如果更新 API 以添加进度报告,我会再次更新。