pico0102
pico0102

Reputation: 59

How to change attributes of a class without having to delete the app using realm

Currently I'm writing a program in Swift using realm. I'm fairly new to iOS development but my understanding of realm is when you change a class stored in realm, you need to delete the app from the device to get rid of the persist data. Unfortunately, I have manually entered in a pretty big database into the app.

Currently I need to change the name of an attribute within a class, but in the future may need to add attributes. What is the best way of updating realm storage so I don't need to delete the app?

Here is one of my models:

class Device: Object {

   dynamic var name = ""
   dynamic var id = ""
   dynamic var os = ""
   dynamic var currentUser: User?
   dynamic var dateStamp = NSDate()
}

Upvotes: 3

Views: 1012

Answers (1)

marius
marius

Reputation: 7806

You can add a migration as seen in our docs and use that to take over the old values to a new property:

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) {
        // The -enumerateObjects:block: method iterates
        // over every Device object stored in the Realm file
        [migration enumerateObjects:Device.className
                      block:^(RLMObject *oldObject, RLMObject *newObject) {
            // e.g. Rename 'os' to 'operatingSystem'
            newObject[@"operatingSystem"] = oldObject[@"os"]
        }];
    }
};

// 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) {
        // The enumerate(_:_:) method iterates
        // over every Device object stored in the Realm file
        migration.enumerate(Device.className()) { oldObject, newObject in
            // e.g. Rename 'os' to 'operatingSystem'
            newObject["operatingSystem"] = oldObject["os"]
        }
    }
  })

// 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: 1

Related Questions