在自动迁移中获取 NSMigrationManager 引用的最快方法?

发布于 2024-10-12 20:05:45 字数 761 浏览 2 评论 0原文

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

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

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

唯一的问题是,当您进行 beginEntityMapping 调用时,进度似乎已达到 30% 左右(此外,这 30% 通常约占总进度的一半)调用 addPersistentStoreWithType 所花费的时间,因此实际上比看起来更糟糕)。

有其他人知道可用于在程序中尽早引用 NSMigrationManager 的任何技巧,这样我就不必错过向用户提供有关应用程序原因的反馈的前三分之一的机会启动需要这么长时间?

预先感谢您的任何帮助!

I have a data-heavy app and I have implemented all my CoreData migration stuff using the visual mapping models in XCode and NSEntityMigrationPolicy implementations for the cleanup code. As it turns out, the migrations on a real device are really lengthy, sometimes taking up to five minutes to complete.

I really need to give feedback to the user when this is going on, and want to KVO the migrationProgress attribute on the NSMigrationManager. The trick is that addPersistentStoreWithType:configuration:URL:options:error: doesn't let you get a reference to the NSMigrationManager in the event that it deems a migration to be necessary.

I discovered that I could get a reference to the NSMigrationManager by implementing the beginEntityMapping:manager:error: callback on my custom NSEntityMigrationPolicy, starting off the observing in there.

The only issue is that by the time you get to the beginEntityMapping call, progress seems to be up to about 30% (also, this 30% typically represents about half the total time spent inside the call to addPersistentStoreWithType, so it's actually even worse than it seems).

Is anyone else aware of any tricks that can be used to get a reference to the NSMigrationManager a bit earlier in proceedings so that I don't have to miss out on the first third of the opportunity to give feedback to the user about why the app is taking so long to start up?

Thanks in advance for any help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时光病人 2024-10-19 20:05:45

经过几周的解决这个问题后,无法找到任何方法来实现这一点,只是完全停止使用 addPersistentStoreWithType:configuration:URL:options:error: 并按照以下方式手动触发迁移文档

After a few more weeks of hacking around this problem, wasn't able to find any way to achieve this and just stopped using addPersistentStoreWithType:configuration:URL:options:error: altogether and manually triggered migration as per the documentation.

泼猴你往哪里跑 2024-10-19 20:05:45

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

 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

如果它是 YES (你真的希望有一个更容易的迁移),你就不会得到任何进度更新,这是有史以来最糟糕的 catch-22 :(

You can do it with code like this:

 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];

But take special note of -usesStoreSpecificMigrationManager

If it's YES (which you really want to have a much easier migration), you get no progress updates which is the worst catch-22 ever :(

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文