This works like a charm for me since Swift 2 introduced try / catch. I just call the testRealmFile()application from my delegate at startup, and after that, everything is cool.
func testRealmFile(){
do {
try Realm().objects(Model1)
try Realm().objects(Model2)
} catch {
print("can't access realm, migration needed")
deleteRealmFile()
}
}
func deleteRealmFile(){
if let path = Realm.Configuration.defaultConfiguration.path {
do{
try NSFileManager.defaultManager().removeItemAtPath(path)
print("realm file deleted")
} catch {
print("no realm file to delete")
}
}
}
source
share