领域错误:迁移

时间:2016-02-20 04:47:07

标签: xcode swift realm

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中添加了一些实例变量,它给了我这个错误。

1 个答案:

答案 0 :(得分:3)

如果你刚刚运行应用程序进行测试并且没有任何重要数据,你想要保留并且不能轻易复制,我建议从用户数据中删除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()