Anirudh
Anirudh

Reputation: 59

Realm Error : Migration

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.'

Any ideas? I downloaded a project from the internet and decided to modify it a bit and play around with it. I added some instance variables to the basic RLMObject and it gives me this error.

Upvotes: 0

Views: 686

Answers (1)

marius
marius

Reputation: 7806

If you have just run the app so far for testing purposes and you don't have any important data, you want to keep and cannot easily reproduce, I'd recommend to delete the Realm from the user data. You can achieve that e.g. by deleting the entire app from your device or simulator. You will start with a clean state and Realm won't know about any previous schema, so you won't need to migrate.


If you have just added new fields to the objects and need to keep the data for some reason, then it is sufficient to add the minimal necessary migration code, as seen in our docs:

Objective-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 (with 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()

Upvotes: 3

Related Questions