Reputation: 540
I am using storyboards and some of the viewcontrollers fetch from the Realm as default property values? Because of that the application access the Realm before application(_:didFinishLaunchingWithOptions:)
is called. So basically exception raised every time app launches and tries to do the Realm migration.
Is there a way to solve this issue?
Further, since we are in dev stage and we don't want to deal with migration every time we made changes to the Realm object models, is there a way to just purge the Realm file and have a fresh start if migration is detected to be needed? I find an issue reported on github (https://github.com/realm/realm-cocoa/issues/1692) but it seems no solution is provided. PS, I am using the latest Realm for iOS.
Upvotes: 1
Views: 158
Reputation: 15991
If you're unable to control which order the storyboards are being loaded automatically by iOS in contrast to the app delegate methods, my recommendation would be to remove the initial storyboard setting from your app's info.plist file and simply manually set up and display it from your app's delegate instead:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let storyboard = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle())
let initialViewController = storyboard.instantiateInitialViewController()
self.window = UIWindow(frame:UIScreen.mainScreen().bounds)
self.window?.rootViewController = initialViewController
self.window?.makeKeyAndVisible()
return true
}
This will let you explicitly control when the storyboard is loaded, letting you do it after your initial Realm setup.
If you're constantly tweaking your model objects during development and don't need to handle migrations just yet, one possible (But slightly hacky) way of doing it is to simply call Realm()
for the first time, and if it throws an exception (Which it will if it needs to perform a migration), catch the exception and use it to simply delete the Realm file from disk.
Upvotes: 5