Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Realm migrations in Swift

I have a Realm Object modeled as so

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 perform a migration.

Within my AppDelegate I have imported RealmSwift.

Within the function didFinishLaunchWithOptions I call

Migrations().checkSchema()

Migrations is a class declared in another file.

Within that file there is a struct declared as so.

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 am getting an error for adding setCount to the model

like image 892
Cody Weaver Avatar asked Jan 20 '16 04:01

Cody Weaver


People also ask

What is realm for Swift?

Realm Swift is an easy to use alternative to SQLite and Core Data that makes persisting, querying, and syncing data as simple as working directly with native Swift objects. Deploy a sample appView documentation.

Does realm work with SwiftUI?

Realm objects have bindings to SwiftUI controls and, much like toggling the bought property, they already start a realm transaction to write the changes in the database whenever you change those values.

What is realm database iOS?

Realm is a fast, scalable alternative to SQLite with mobile to cloud data sync that makes building real-time, reactive mobile apps easy. Deploy a sample appView documentation.


1 Answers

You will need to invoke the migration. Merely creating a configuration, won't invoke it. There are two ways of doing this:

  1. Set your configuration with migration as Realm's default configuration-

    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. Migrate manually using migrateRealm :

migrateRealm(config)

Now your migration should work properly.

like image 68
Shripada Avatar answered Oct 15 '22 06:10

Shripada