Remove property in Realm object

5.6k views Asked by At

I'm trying to remove a property in one of my Realm objects however I'm not sure how to write a migration for this.

I just removed the property from my object's header file but that didn't work as I get this error:

Terminating app due to uncaught exception 'RLMException', reason: 'Migration is required for object type 'Stock' due to the following errors: - Property 'percentageOn' is missing from latest object model.'

I know how to write a migration add fields, but how do I remove one?

1

There are 1 answers

2
TiM On

What David said is correct. If you make sure to perform a migration properly, then Realm can easily handle properties that have been removed as well as added. Unless you actually still need the value in percentageOn, you can even leave the migration block empty like in the example on the Realm website:

// Inside your [AppDelegate didFinishLaunchingWithOptions:]

// Notice setSchemaVersion is set to 1, this is always set manually. It must be
// higher than the previous version (oldSchemaVersion) or an RLMException is thrown
[RLMRealm setSchemaVersion:1
            forRealmAtPath:[RLMRealm defaultRealmPath] 
        withMigrationBlock:^(RLMMigration *migration, NSUInteger 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
  }
}];

// now that we have called `setSchemaVersion:withMigrationBlock:`, opening an outdated
// Realm will automatically perform the migration and opening the Realm will succeed
[RLMRealm defaultRealm];