Swift problems with migration

Hi

Used Realm (locally) on my first ios app. All worked fine. I’m now working on an update which requires me adding more fields to my core object that I store in Realm. Its a ‘Film’ object and I want to add extra fields for ‘release year’ and ‘imdbID’ - both strings.

I add them to the class and then get ‘migration required’ errors. This is new to me, but I’ve done my homework on the internet and read a lot of articles but can’t seem to get it to work.

from inside my application(application:didFinishLaunchingWithOptions:). function I call this routine

 func configureMigration() {
    
    let vers = UInt64(2)
    let config = Realm.Configuration( schemaVersion: vers, migrationBlock: { migration, oldSchemaVersion in
         print("oldSchemaVersion: \(oldSchemaVersion)")
         if (oldSchemaVersion != vers) {
            print("  performing migration")

            migration.enumerateObjects(ofType: Film.className()) { (_, newFim) in
                newFim?["imdbID"] = ""
                newFim?["releaseYear"] = ""
            }
         }
     })
    
    Realm.Configuration.defaultConfiguration = config

    do {
        _ = try Realm()
    } catch let _ as NSError {
        // print error
    }
}

I can see its called by using break points but it never gets to the migration bit, as I get the same migration error from the first ViewController that tries to use Realm first.

Couple of things to point out…

  • I currently have users with data stored so deleting every thing and starting a new DB is not a solution
  • I never initially set a schema version, but its giving me a number of UInt64 4371128885
  • the reason I have this code “oldSchemaVersion != vers” is just to try and force the migration as a test
  • in the view controller I already try and lazy load my realm with
    lazy var realm:Realm = {
    return try ! Realm()
    }()

but that gives the original migration code 10 error.

If it makes any difference all the fields I’m trying to add to my object are optional, and I check them before using them.

Any help would be great as I’m just going around in circles at the moment.

Thanks
Chris

I copy and pasted your code intro a project and it works perfectly for me.

The only thing I changed was, starting with fresh data, setting the vers to 1. Then I added a couple of fields to Film, set the vers to 2 and it worked.

My guess is the order in which Realm is being accessed.

Don’t access Realm in viewDidLoad() or viewWillAppear() (or any view loading function) of your first view controller (macOS) because they are called before applicationDidFinishLaunching and it won’t give the migration block an opportunity to run and do the migration.

You could do this in the first viewController if needed:

    //MARK: - init
    override func viewDidLoad() {
        super.viewDidLoad()

        self.performMigration()
    }
    
    func performMigration() {

Thanks for taking a look - will try and implement as you suggest. With regard to version numbers if I check mine I get 4371128885, although I’ve never explicity set it to anything, before trying to release this new version. Is there a way to reset it without deletign users local data and making them start again?