Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Realm schema version 0 is less than last set version 1 Swift

I updated my data model and wrote the migration in application: didFinishLaunchingWithOptions per Realm documentation:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

    let config = Realm.Configuration(

        schemaVersion: 1,


        migrationBlock: { migration, oldSchemaVersion in

            if (oldSchemaVersion < 1) {

            }
    })

           Realm.Configuration.defaultConfiguration = config

        let realm = try! Realm()

My initial VC has a tableView and is embedded in a navigation controller that calls the realm as shown below:

class AllRoundsVC: UIViewController, UITableViewDelegate, UITableViewDataSource {
let realm = try! Realm()

When I leave it like this, my app crashes upon launch stating "fatal error: 'try!' expression unexpectedly raised an error: Error Domain=io.realm Code=0 "Provided schema version 0 is less than last set version 1." UserInfo={NSLocalizedDescription=Provided schema version 0 is less than last set version 1.}

However, when I put a dummy VC infront of the navigation controller with a simple button to segue to the navigation controller everything works fine. I know this has to do with appDidFinishLaunchingWithOptions loading before or after initial VC.

My question is, how can I stop from crashing while still calling try! Realm() on my initial VC? I don't want to keep a pointless VC infront of the tableView just to load the proper schema.

Thanks

like image 624
JustinM Avatar asked May 12 '16 00:05

JustinM


1 Answers

after doing some realm research on an unrelated issue I came across a solution to this problem. Here is a link to the solution! link

How can I get the migration to run before the app starts to run the code?

like image 118
JustinM Avatar answered Oct 29 '22 01:10

JustinM