【问题标题】:Realm Error : Migration领域错误:迁移
【发布时间】:2016-02-20 04:47:07
【问题描述】:
2016-02-19 20:42:47.857 BizCardz[78363:2192098] *** 
Terminating app due to uncaught exception 'RLMException',
reason: 'Migration is required for object type 'Card' 
due to the following errors:
- Property 'position' has been added to latest object model.
- Property 'email' has been added to latest object model.
- Property 'phoneNumber' has been added to latest object model.
- Property 'address' has been added to latest object model.
- Property 'website' has been added to latest object model.'

有什么想法吗?我从互联网上下载了一个项目,并决定对其进行一些修改并尝试一下。我在基本 RLMObject 中添加了一些实例变量,它给了我这个错误。

【问题讨论】:

  • 您是否查看过如何在 Realm doco 中进行迁移?这很简单。

标签: xcode swift realm


【解决方案1】:

如果您到目前为止只是出于测试目的运行该应用程序并且您没有任何重要数据,并且想要保留并且无法轻松复制,我建议您从用户数据中删除 Realm。你可以做到这一点,例如通过从您的设备或模拟器中删除整个应用程序。你将从一个干净的状态开始,Realm 不会知道任何以前的模式,所以你不需要迁移。


如果您刚刚向对象添加了新字段并且由于某种原因需要保留数据,那么添加最少的必要迁移代码就足够了,如our docs 所示:

目标-C

// Inside your [AppDelegate didFinishLaunchingWithOptions:]

RLMRealmConfiguration *config = [RLMRealmConfiguration defaultConfiguration];
// Set the new schema version. This must be greater than the previously used
// version (if you've never set a schema version before, the version is 0).
config.schemaVersion = 1;

// Set the block which will be called automatically when opening a Realm with a
// schema version lower than the one set above
config.migrationBlock = ^(RLMMigration *migration, uint64_t oldSchemaVersion) {
  // We haven’t migrated anything yet, so oldSchemaVersion == 0
  if (oldSchemaVersion < 1) {
    // Nothing to do!
    // Realm will automatically detect new properties and removed properties
    // And will update the schema on disk automatically
  }
};

// Tell Realm to use this new configuration object for the default Realm
[RLMRealmConfiguration setDefaultConfiguration:config];

// Now that we've told Realm how to handle the schema change, opening the file
// will automatically perform the migration
[RLMRealm defaultRealm];

Swift(使用 Realm Swift)

https://realm.io/docs/swift/latest/#performing-a-migration

// Inside your application(application:didFinishLaunchingWithOptions:)

let config = Realm.Configuration(
  // Set the new schema version. This must be greater than the previously used
  // version (if you've never set a schema version before, the version is 0).
  schemaVersion: 1,

  // Set the block which will be called automatically when opening a Realm with
  // a schema version lower than the one set above
  migrationBlock: { migration, oldSchemaVersion in
    // We haven’t migrated anything yet, so oldSchemaVersion == 0
    if (oldSchemaVersion < 1) {
      // Nothing to do!
      // Realm will automatically detect new properties and removed properties
      // And will update the schema on disk automatically
    }
  })

// Tell Realm to use this new configuration object for the default Realm
Realm.Configuration.defaultConfiguration = config

// Now that we've told Realm how to handle the schema change, opening the file
// will automatically perform the migration
let realm = try! Realm()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-05
    相关资源
    最近更新 更多