Remove property in Realm object

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

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

Application terminated due to uncaught 'RLMException', reason: 'The Inventory object type requires migration due to the following errors: - Property' percentOn 'is missing from the latest object model.'

I know how to write add hyphen fields, but how do I remove one of them?

+3


source to share


1 answer


What David said is true. If you migrate correctly, Realm can easily handle properties that have been removed as well as added. If you don't actually need the value in yet percentageOn

, you can even leave the migration block empty, as 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];

      

+4


source







All Articles