alex1511
alex1511

Reputation: 445

Realm Runtime Error

I recently upgraded to Swift 2.0 and now I have been experiencing problems with realm. Most recently, I am experiencing an issue where the app instantly crashes when it reaches the first occurrence of a "try! Realm()" resulting in this error:

fatal error: 'try!' expression unexpectedly raised an error: Error Domain=io.realm Code=2 "open() failed: No such file or directory" UserInfo={NSFilePath=/Users/XXXXX/Library/Developer/CoreSimulator/Devices/7299DF18-E7D5-4499-93DD-A5035FB48E67/data/Containers/Data/Application/BED64819-5895-407F-9E90-9888741E24EB/Documents/default.realm, NSLocalizedDescription=open() failed: No such file or directory, Error Code=2}: file /Library/Caches/com.apple.xbs/Sources/swiftlang/swiftlang-700.0.59/src/swift/stdlib/public/core/ErrorType.swift, line 50 (lldb)

I saw one other post somewhat related to this, but it did not help because I am not trying to call the path directly, it is just throwing this error.

Thank you

Upvotes: 1

Views: 1093

Answers (2)

artemchen
artemchen

Reputation: 169

Same thing happened to me when I manually deleted an object from Realm via Realm browser. Here are my two cents: Deleting realm.lock and other log files and relaunching an app worked for me. Take a look at the screenshot:

enter image description here

Upvotes: -1

jpsim
jpsim

Reputation: 14409

When you use try! in Swift, you're choosing to ignore errors that you could otherwise recover from.

In this case, the Realm initializer is marked as throws. Here's an excerpt from Realm's docs on Error Handling:

Like any disk IO operation, creating a Realm instance could sometimes fail if resources are constrained. In practice, this can only happen the first time a Realm instance is created on a given thread. Subsequent accesses to a Realm from the same thread will reuse a cached instance and will always succeed.

To handle errors when first accessing a Realm on a given thread, use Swift’s built-in error handling mechanism:

do {
  let realm = try Realm()
} catch let error as NSError {
  // handle error
}

Upvotes: 3

Related Questions