3

What are the steps for migrating Realm DB changes for updated iOS apps already shipped?

Is there any prior step one should have done before shipping a Realm.io database app?

Here are the similar question with respect to core-data Steps to migrate Core Data databases for shipped iPhone apps, but I am looking for migrating Realm database.

Here goes the crash logs:

*** Terminating app due to uncaught exception 'RLMException', reason: 'Migration is required for object type 'ExampleRealm' due to the following errors: - Property 'values' has been added to latest object model.'

Community
  • 1
  • 1
sKhan
  • 9,694
  • 16
  • 55
  • 53
  • It probably depends on what your data store looks like now. Do you have your data in a Core Data store or in NSUserDefaults? Other than that, look at the Realm getting started guide. It's pretty good. – Moshe Feb 01 '16 at 00:31
  • No core-data is been used. Since the app is still under development and while deploying the ad-hoc build with changes to realm database structure. App get crash unless we do a fresh install. – sKhan Feb 01 '16 at 00:34
  • What sort of crash? Can you share a log in your original question? – Moshe Feb 01 '16 at 00:35
  • yep, logs has been added. – sKhan Feb 01 '16 at 00:40
  • Looks like you need to look up how to do a migration in Realm. Realm is complaining that one of your properties was added to the latest object model. – Moshe Feb 01 '16 at 00:47
  • yeah that is what exactly I am looking for how to do a migration in Realm – sKhan Feb 01 '16 at 00:49
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/102184/discussion-between-shaf-khan-and-moshe). – sKhan Feb 01 '16 at 00:59

1 Answers1

3

According to the Realm documentation, there are some samples of how you do a migration in Realm.

From the sample code, there's this:

// define a migration block
// you can define this inline, but we will reuse this to migrate realm files from multiple versions
// to the most current version of our data model
RLMMigrationBlock migrationBlock = ^(RLMMigration *migration, uint64_t oldSchemaVersion) {
    if (oldSchemaVersion < 1) {
        [migration enumerateObjects:Person.className block:^(RLMObject *oldObject, RLMObject *newObject) {
            if (oldSchemaVersion < 1) {
                // combine name fields into a single field
                newObject[@"fullName"] = [NSString stringWithFormat:@"%@ %@", oldObject[@"firstName"], oldObject[@"lastName"]];
            }
        }];
    }
    if (oldSchemaVersion < 2) {
        [migration enumerateObjects:Person.className block:^(RLMObject *oldObject, RLMObject *newObject) {
            // give JP a dog
            if ([newObject[@"fullName"] isEqualToString:@"JP McDonald"]) {
                Pet *jpsDog = [[Pet alloc] initWithValue:@[@"Jimbo", @(AnimalTypeDog)]];
                [newObject[@"pets"] addObject:jpsDog];
            }
        }];
    }
    if (oldSchemaVersion < 3) {
        [migration enumerateObjects:Pet.className block:^(RLMObject *oldObject, RLMObject *newObject) {
            // convert type string to type enum if we have outdated Pet object
            if (oldObject && oldObject.objectSchema[@"type"].type == RLMPropertyTypeString) {
                newObject[@"type"] = @([Pet animalTypeForString:oldObject[@"type"]]);
            }
        }];
    }
    NSLog(@"Migration complete.");

It looks like you declare a block in which you enumerate objects and manually update the schema.

Moshe
  • 57,511
  • 78
  • 272
  • 425