如何从Core Data自动轻量级迁移到手动? [英] How to switch from Core Data automatic lightweight migration to manual?

查看:242
本文介绍了如何从Core Data自动轻量级迁移到手动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的情况是与此问题类似。我使用轻量级迁移与以下代码,苹果文档和其他SO线程相当的香草。它在初始化Core数据栈时运行应用程序启动。

My situation is similar to this question. I am using lightweight migration with the following code, fairly vanilla from Apple docs and other SO threads. It runs upon app startup when initializing the Core Data stack.

NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
    [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
    [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption,
    nil];

NSError *error = nil;

NSString *storeType = nil;
if (USE_SQLITE) { // app configuration
    storeType = NSSQLiteStoreType;
} else {
    storeType = NSBinaryStoreType;
}

persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];

// the following line sometimes crashes on app startup
if (![persistentStoreCoordinator addPersistentStoreWithType:storeType configuration:nil URL:[self persistentStoreURL] options:options error:&error]) {
    // handle the error
}

对于某些用户,

我知道一个修复是将此切换到手动映射和迁移。这是什么食谱?对我来说,很长的路要走所有的苹果文档,但我不记得有很好的示例和教程专门为模式迁移。

I understand that a fix is to switch this to manual mapping and migration. What is the recipe to do that? The long way for me would be to go through all Apple docs, but I don't recall there being good examples and tutorials specifically for schema migration.

推荐答案

有关如何进行手动迁移的很好的示例,但基本步骤是:

There are good examples on how to do a manual migration but the basic steps are:


  • 创建映射模型

  • 将您的项目包含在映射模型中

  • 关闭自动迁移

但是,崩溃是什么?你被操作系统杀死太长时间了吗?作为手动迁移不会治愈。

However, what is the crash? Are you being killed by the OS for taking too long? As a manual migration will not cure that.

您使用什么类型的后备存储?如果您使用的是二进制文件,则可能会丢失内存,因为迁移将在内存中有两个完整数据库。

What type of backing store are you using? If you are using binary it is possible to blow out memory because a migration will have two copies of the entire database in memory.


我认为我已经看到了这种情况也发生在SQLite,所以也许有一些东西比内存,虽然这是一个有效的点。是的,它被杀死了操作系统花费太长时间,特别是在1G设备上。我认为手动迁移也是一种方法,从在具有超时限制的应用程序启动迁移到没有时间限制的东西,并且可能在后台线程等。 ?

I think I have seen this happen also with SQLite, so maybe there is something more than memory, although that is a valid point. Yes, it is killed by OS for taking too long, especially on 1G devices. I thought that manual migration was also a way to move from "migrate on app startup which has timeout constraints" to something which has no time constraints, and do it maybe on background thread or such. ?

由于您正在更改数据源,因此无法在后台线程上执行迁移。如果您的迁移在启动时耗时过长,建议的解决方案是:

There is no way to do the migration on a background thread because you are changing the source of the data. If your migration is taking too long on start up then the recommended solution is:


  • -applicationDidFinishLaunching: / code>循环通过运行循环完成,而无需触摸Core Data。

  • 通过运行循环在下一次迭代时启动迁移(可能要向用户显示某种进度指示器),并完成迁移。

如果您使用的是二进制存储,您可能仍然会遇到内存问题,但这将解决您启动时的崩溃问题。崩溃是操作系统说我想你已经锁定。通过让 -applicationDidFinishLaunching:完成你告诉操作系统你还没有锁定。

If you are using a binary store you are probably still going to have memory issues, but this will solve your crash on launch. The crash is the OS saying "I think you have locked up". By letting the -applicationDidFinishLaunching: complete you are telling the OS that you have not locked up.

这篇关于如何从Core Data自动轻量级迁移到手动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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