Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Steps to migrate Realm databases for shipped iPhone apps

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

like image 940
sKhan Avatar asked Nov 24 '25 08:11

sKhan


1 Answers

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.

like image 100
Moshe Avatar answered Nov 26 '25 20:11

Moshe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!