如何迁移未存储的 Realm 对象?
How to migrate Realm object that hasn't been stored?
我有一个对象(还)没有存储在任何地方。即使没有为它创建 Realm,应用程序也会崩溃,并在我修改它后提示该对象需要迁移。
我试过这个(方法func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool
):
RLMRealm.setSchemaVersion(1, forRealmAtPath: RLMRealm.defaultRealmPath()) { (migration: RLMMigration!, oldSchemaVersion: UInt) -> Void in
if oldSchemaVersion < 1 {
// not needed, nothing stored...
}
}
RLMRealm.defaultRealm()
日志:
*** Terminating app due to uncaught exception 'RLMException', reason: 'Migration is required for object type 'PYDRealmChange' due to the following errors:
- Property 'relativePath' is missing from latest object model.
- Property 'source' has been added to latest object model.
- Property 'target' has been added to latest object model.'
这是我在文档中找到的内容。它没有帮助,应用程序仍在崩溃。我的猜测是我需要 actual 领域来执行迁移。所以我的问题是 - 如果有 none,我该如何迁移?
所以问题解决了:
我不知道的事实是,我猜测模型对象设置在 all 应用程序中存在的领域,无论我是否将其存储在他们与否。解决方案是 copy/paste 我应用中所有领域对象的代码 RLMRealm.setSchemaVersion...
。然后它开始工作了。
我有一个对象(还)没有存储在任何地方。即使没有为它创建 Realm,应用程序也会崩溃,并在我修改它后提示该对象需要迁移。
我试过这个(方法func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool
):
RLMRealm.setSchemaVersion(1, forRealmAtPath: RLMRealm.defaultRealmPath()) { (migration: RLMMigration!, oldSchemaVersion: UInt) -> Void in
if oldSchemaVersion < 1 {
// not needed, nothing stored...
}
}
RLMRealm.defaultRealm()
日志:
*** Terminating app due to uncaught exception 'RLMException', reason: 'Migration is required for object type 'PYDRealmChange' due to the following errors:
- Property 'relativePath' is missing from latest object model.
- Property 'source' has been added to latest object model.
- Property 'target' has been added to latest object model.'
这是我在文档中找到的内容。它没有帮助,应用程序仍在崩溃。我的猜测是我需要 actual 领域来执行迁移。所以我的问题是 - 如果有 none,我该如何迁移?
所以问题解决了:
我不知道的事实是,我猜测模型对象设置在 all 应用程序中存在的领域,无论我是否将其存储在他们与否。解决方案是 copy/paste 我应用中所有领域对象的代码 RLMRealm.setSchemaVersion...
。然后它开始工作了。