Rocky Smith
Rocky Smith

Reputation: 195

Crash when using deleteRealmIfMigrationNeeded with Realm in Swift

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)

Upvotes: 10

Views: 4367

Answers (2)

David Vanture
David Vanture

Reputation: 1

I discovered that turning off encryption when setting up the Realm will allow you to have the Realm Browser open at the same time.

Upvotes: 0

Michael
Michael

Reputation: 1285

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!

Upvotes: 34

Related Questions