Lachtan
Lachtan

Reputation: 5172

Unable to read data from Realm file

I want to use realm file with implicit data in my app (In separate project I filled it with data, then made copy of it. Model object is same in both apps).

On simulator, everything is just fine. But when I run app on iPhone, Xcode throws me error.

let path = (NSBundle.mainBundle().pathForResource("testLevel", ofType: "realm"))!
let config = Realm.Configuration(path: path)
let realm = try! Realm(configuration: config) // also tried try! Realm(path: path)

When I print path to .realm file, everything's fine - no nil -

Dont know how to handle it, any ideas? (iOS9)

Error:

fatal error: 'try!' expression unexpectedly raised an error: Error Domain=io.realm Code=2 "Operation not permitted" UserInfo={Error Code=2, NSFilePath=/var/containers/Bundle/Application/7DE151B5-42EE-45C6-8245-B57683EA64D8/sneakers.app/testLevel.realm, NSLocalizedDescription=Operation not permitted}: file /Library/Caches/com.apple.xbs/Sources/swiftlang/swiftlang-703.0.18.1/src/swift/stdlib/public/core/ErrorType.swift, line 54

Upvotes: 1

Views: 1365

Answers (1)

TiM
TiM

Reputation: 16021

The Resources folder of your app is read-only, so you can't directly open a writeable Realm file from there.

You'll need to copy it to a directory in which your app has write access (e.g the 'Documents' or 'Application Support' directories) and then try and open it from there. :)

Upvotes: 4

Related Questions