2

我有一个数据量很大的应用程序,并且我已经使用 XCode 中的可视映射模型和 NSEntityMigrationPolicy 实现中的清理代码实现了我所有的 CoreData 迁移内容。事实证明,真实设备上的迁移非常耗时,有时需要长达五分钟才能完成。

当这种情况发生时,我真的需要向用户提供反馈,并且想要 KVO migrationProgressNSMigrationManager 上的属性。诀窍是,addPersistentStoreWithType:configuration:URL:options:error:如果它认为有必要进行迁移,它不会让您获得对 NSMigrationManager 的引用。

我发现我可以通过beginEntityMapping:manager:error:在我的自定义 NSEntityMigrationPolicy 上实现回调来获得对 NSMigrationManager 的引用,从那里开始观察。

唯一的问题是,当您beginEntityMapping接听电话时,进度似乎高达 30% 左右(此外,这 30% 通常代表在通话中花费的总时间的一半addPersistentStoreWithType左右,所以它实际上比看起来更糟糕)。

有没有其他人知道任何技巧可以用来在程序的早期获得对 NSMigrationManager 的引用,这样我就不必错过前三分之一的机会向用户提供关于为什么应用程序的反馈启动需要这么长时间吗?

提前感谢您的帮助!

4

2 回答 2

0

在解决这个问题几周后,无法找到任何方法来实现这一点,只是完全停止使用并根据文档addPersistentStoreWithType:configuration:URL:options:error:手动触发迁移。

于 2011-02-07T18:37:38.433 回答
0

你可以用这样的代码来做到这一点:

 NSMigrationManager *migrationManager = [[NSMigrationManager alloc] initWithSourceModel:sourceModel
                                                                      destinationModel:destinationModel];

//if it's set to NO, we can't migrate due to too much memory
//if it's set to YES (the default), we get no progress reporting!!
//migrationManager.usesStoreSpecificMigrationManager = NO;

NSError *mappingError;
NSMappingModel *mappingModel = [NSMappingModel inferredMappingModelForSourceModel:sourceModel
                                                                 destinationModel:destinationModel
                                                                            error:&mappingError];

NSPersistentStore *persistentStore;
NSError *addPersistentStoreError;
if (mappingModel) {

    NSError *migrationError;
    BOOL migrationSuccess = [migrationManager migrateStoreFromURL:sourceStoreURL
                                                             type:NSSQLiteStoreType
                                                          options:nil
                                                 withMappingModel:mappingModel
                                                 toDestinationURL:destinationStoreURL
                                                  destinationType:NSSQLiteStoreType
                                               destinationOptions:nil error:&migrationError];

但要特别注意 -usesStoreSpecificMigrationManager

如果是(您真的希望迁移更容易),您将不会获得任何进度更新,这是有史以来最糟糕的 catch-22 :(

于 2015-06-04T15:27:36.120 回答