1

I don't know if I'm missing something here.

I got a crash after running my app without a migration block after updating a property (same problem as here Realm Migration not working)

But now when I run the app, it must run the migration because it no longer crashes but my object's properties are not updated.

I have updated the below object ("minReps" is the one I've added):

class ExerciseGeneratorObject: Object {
    @objc dynamic var name = ""
    @objc dynamic var minReps = 0
    @objc dynamic var maxReps = 0

    convenience init(name: String, minReps: Int, maxReps: Int) {
        self.init()
        self.name = name
        self.minReps = minReps
        self.maxReps = maxReps
    }

Then I'm running an empty migration block like this in appDelegate :

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    let config = Realm.Configuration(

        schemaVersion: 3,

        migrationBlock: { migration, oldSchemaVersion in

            if (oldSchemaVersion < 3) {

            }
    })

    Realm.Configuration.defaultConfiguration = config

    let realm = try! Realm()

I thought Realm was meant to update object properties automatically if you run an empty migration block - is that wrong? Am I missing some code to make this work?

There's a very similar problem here (Realm migrations in Swift) (the isn't me!) but looks out of date now (and sure I've tried the solutions there as above!)

nc14
  • 539
  • 1
  • 8
  • 26

1 Answers1

4

Current schema version should be set in the app via realm configuration, and you should increase it, in your code you set schema version to 3, and asking realm to migrate realm if oldSchemaVersion less than 3, set schema version to 4, and it will work

    var 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: 4,

        // 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 < 3) {
                // Nothing to do!
                // Realm will automatically detect new properties and removed properties
                // And will update the schema on disk automatically
            }
    })

    Realm.Configuration.defaultConfiguration = config
    config = Realm.Configuration()
    config.deleteRealmIfMigrationNeeded = true
A.Munzer
  • 1,890
  • 1
  • 16
  • 27
  • I've tried this in my app delegate and changed the schema again but it still doesn't work - is there a way to check current schema version? I wonder if i've accidentally changed that somewhere? is it right i'm using let realm = try! Realm() on each page that I use it as well? – nc14 May 09 '18 at 09:35
  • this worked perfectly thanks - obviously i was missing the delete realm bit ! – nc14 May 09 '18 at 10:03