iPhone应用程序启动时间和Core Data迁移 [英] iPhone app launch times and Core Data migration

查看:107
本文介绍了iPhone应用程序启动时间和Core Data迁移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个核心数据应用程序,我计划用一个新的模式更新。轻量级迁移似乎工作,但它需要时间与数据库中的数据量成比例。这发生在应用程序的 didFinishLaunchingWithOptions 阶段。

I have a Core Data application which I plan to update with a new schema. The lightweight migration seems to work, but it takes time proportional to the amount of data in the database. This occurs in the didFinishLaunchingWithOptions phase of the app.

我想避免 app>未能及时启动问题,因此我假设我无法在 didFinishLaunchingWithOptions 方法中保留迁移。

I want to avoid <app> failed to launch in time problems, so I assume I cannot keep the migration in the didFinishLaunchingWithOptions method.

我假设最好的方法将涉及在后台线程中执行迁移。我假设还需要推迟加载主ViewController,直到加载完成,以避免在初始化完成之前使用 managedObjectContext

I assume the best method would involve performing the migration in a background thread. I assume also that I'd need to defer loading of the main ViewController until the loading completes to avoid using the managedObjectContext until initialization completes.

这是否有意义,并且有这种初始化的示例代码(可能在Apple示例项目中)?

Does this make sense, and is there example code (maybe in Apple sample projects) of this sort of initialization?

推荐答案

您不能将迁移放在 NSOperation 中,因为它需要在主线程上运行。你需要做的是走出 -applicationDidFinishLaunching:方法,而不用触及Core Data栈。如果你可以快速完成那个方法(和那个运行循环),那么你的应用程序不会被终止,你可以采取只要用户将完成你的迁移。

You can't put migration in a NSOperation because it will need to run on the main thread. What you need to do is to get out of the -applicationDidFinishLaunching: method without touching the Core Data stack. If you can finish that method (and that run loop cycle) quickly then your app will not be terminated and you can take as long as the user will put up with to finish your migration.

请参阅我的回答:如何从Core Data自动轻量级迁移切换到手动?

See my answer here: How to switch from Core Data automatic lightweight migration to manual?

澄清我对此的立场。它本质上可能是做任何事情。但是,在后台线程中进行迁移是一个坏主意。这是非常困难的,以保证堆栈将<永远不会在移植过程中以及其他线程的特定并发症的一个主机被感动。

To clarify my position on this. It is inherently possibly to do just about anything. However, doing a migration on a background thread is a bad idea. It is extremely difficult to guarantee that the stack will never get touched during the migration as well as a whole host of other threading specific complications.

这是可能的,但它涉及高度的风险是完全不必要的。主线程可以并且应该用于执行主Core数据栈的迁移。很容易建立一个模态对话框,让用户知道正在进行迁移,然后在主线程上执行迁移。

It is possible to do it but it involves a high degree of risk that is completely unnecessary. The main thread can and should be used to do a migration of the main Core Data stack. It is easy to put up a modal dialog to let the user know a migration is occurring and then perform the migration on the main thread.

如果您处于这种情况,您的迁移需要花费很长时间,因此强烈建议您使用映射模型从自动迁移切换到手动迁移,以便您可以:

If you are in a situation where your migrations are taking a signficant amount of time then it is also highly recommended that you switch from automatic migration to manual migration with a mapping model so that you can:


  • 如果需要,可以轻松地退出迁移。

  • 在用户退出应用程序时执行迁移。


自从最初回答以来,有很多变化。

A lot has changed since this was originally answered.

现在迁移的答案是在后台队列通过 NSPersistentStoreCoordinator addStore ... dispatch_async 调用)。

The answer now for migrations is to fire them on a background queue (via dispatch_async of the NSPersistentStoreCoordinator's addStore... call).

这也意味着您需要确保您的UI可以处理持久层为空/不可用的未知时间段。这取决于你的应用程序。

This also means that you need to make sure your UI can handle the persistence layer being empty/not available for an unknown period of time. How to do that depends on your application.

例如,你可以有一个临时UI显示舞蹈猫,而持久层执行迁移。

For example, you can have a temporary UI that shows dancing cats while the persistence layer does the migration. The user experience is up to you.

但是,您可以希望让用户在迁移过程中创建数据。这将使以后很难合并(如果有的话)。

However, you do NOT want to let the user create data while the migration is happening. That will make it difficult to merge later (if at all).

这篇关于iPhone应用程序启动时间和Core Data迁移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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