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
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With