Migrating Worlds to Swift

I have a Realm object modeled this way

class WorkoutSet: Object { // Schema 0 dynamic var exerciseName: String = "" dynamic var reps: Int = 0 // Schema 0 + 1 dynamic var setCount: Int = 0 } 

I am trying to migrate.

In my AppDelegate I imported RealmSwift .

Inside the didFinishLaunchWithOptions function didFinishLaunchWithOptions I call

 Migrations().checkSchema() 

Migrations are a class declared in another file.

Inside this file there is a structure declared as such.

 func checkSchema() { 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 switch oldSchemaVersion { case 1: break default: // Nothing to do! // Realm will automatically detect new properties and removed properties // And will update the schema on disk automatically self.zeroToOne(migration) } }) } func zeroToOne(migration: Migration) { migration.enumerate(WorkoutSet.className()) { oldObject, newObject in let setCount = 1 newObject!["setCount"] = setCount } } 

I get an error to add setCount to the model

+7
migration swift data-migration realm data-management
source share
2 answers

You will need to trigger a migration. Simply creating a configuration will not invoke it. There are two ways to do this:

  • Set the migration configuration as the default configuration of Realm -

     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 if oldSchemaVersion < 1 { migration.enumerate(WorkoutSet.className()) { oldObject, newObject in newObject?["setCount"] = setCount } } } ) Realm.Configuration.defaultConfiguration = config 

OR

  1. Manual migration manually using migrateRealm:

migrateRealm (configuration)

Your migration should now work properly.

+15
source share

Because you just create Realm.Configuration . If necessary, the migration block is called by Realm. You cannot directly refer to migration.

So, to call the migration block, you must install the configuration object in Realm or set as the default configuration. Then create an instance of Realm.

So you need to do the following:

 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 switch oldSchemaVersion { case 1: break default: // Nothing to do! // Realm will automatically detect new properties and removed properties // And will update the schema on disk automatically self.zeroToOne(migration) } }) let realm = try! Realm(configuration: config) // Invoke migration block if needed 
+3
source share

All Articles