Migrating a new class to the database


#1

Hi All,

I get the following error when I open the tab that utilizes a class that was added in the last update:

Terminating app due to uncaught exception ‘RLMException’, reason: ‘Can only add, remove, or create objects in a Realm in a write transaction - call beginWriteTransaction on an RLMRealm instance first.’

My migration code is this:

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 enumerateObjects(ofType:_:) method iterates
                // over every Person object stored in the Realm file
                
                let migrationBlock: MigrationBlock = { migration, oldSchemaVersion in
                    migration.enumerateObjects(ofType: AllServices.className()) { oldObject, newObject in
                        if oldSchemaVersion < 1 {
                            let AllServices = newObject?["AllServices"] as? List<MigrationObject>
                            // Add new objects to 'areas' as needed
                            // Add new objects
                            let everything = newObject!["everything"] as! List<Registry>
                            let eucharist = newObject!["eucharist"] as! List<Registry>
                            let weekdayEucharist = newObject!["weekdayEucharist"] as! List<Registry>
                            let office = newObject!["office"] as! List<Registry>
                            let weekdayOffice = newObject!["weekdayOffice"] as! List<Registry>
                            let requiem = newObject!["requiem"] as! List<Registry>
                            let burial = newObject!["burial"] as! List<Registry>
                            let nuptial = newObject!["nuptial"] as! List<Registry>
                            let marriage = newObject!["marriage"] as! List<Registry>
                            let other = newObject!["other"] as! List<Registry>
                        }
                    }
                }
                
            }
            
    })
    
    // 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()
    return true
}

Not sure where I am going wrong, any thought will be greatly appreciated!
Thanks. Blessings,
—Mark


#2

I discovered the answer to my crash, I forgot the write statement for Realm. Doh!

I also discovered a new model/class does not need migration. The documents might want to explore migration in a little more detail!

Hope this helps other new users of Realm.
Blessings,
—Mark