Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Crash when using deleteRealmIfMigrationNeeded with Realm in Swift

Tags:

swift

realm

I'm doing the following inside of didFinishLaunchingWithOptions:

let config = Realm.Configuration(
  schemaVersion: 0,
  deleteRealmIfMigrationNeeded: true
)
Realm.Configuration.defaultConfiguration = config

let realm = try! Realm()

Basically, while developing, I don't want to worry about migrations and just want to clear the database whenever the schema changes. My understanding is that's exactly what deleteRealmIfMigrationNeeded is for.

The problem is that sometimes it crashes while trying to initialize Realm with the following error:

fatal error: 'try!' expression unexpectedly raised an error: Error Domain=io.realm Code=5 "Directory at path '/Users/rock/Library/Developer/CoreSimulator/Devices/D626848E-14D5-47AC-8FFB-9B67D024DEF1/data/Containers/Data/Application/6F71103C-9E10-4131-BED4-D96445FABA52/Documents/default.realm' does not exist."

The default.realm file is getting removed, presumably because of deleteRealmIfMigrationNeeded, but then isn't getting recreated (as I'd expect) when initializing Realm with that last line.

Interestingly, if I manually delete default.realm.lock and then restart the app, it'll work.

Am I doing this wrong? Could this be a bug? (I'm using Realm Swift 2.4.1)

like image 378
Rocky Smith Avatar asked Feb 07 '17 20:02

Rocky Smith


1 Answers

I just came across the same problem and the solution in my case was to turn off the Realm Browser if you have it open.

Cheers!

like image 55
Michael Avatar answered Oct 29 '22 16:10

Michael