Search code examples
realmrealm-migration

Error with Realm, on iOS devices only, after migrating from Swift 1.2 to Swift 2


An other problem when migrating from Swift 1 to Swift 2 and so migrating Realm... In the old code I had this lines of codes that works like a charm :

ApplicationController.A._initRealm = Realm.init(path: NSBundle.mainBundle().pathForResource("initRealm",ofType:"realm")!, readOnly: true, encryptionKey: nil, error: &error)

After the migration process I have replaced this lines of code with:

do
{
    ApplicationController.A._initRealm = try Realm.init(path: NSBundle.mainBundle().pathForResource("initRealm", ofType: "realm")!)
}
catch
{
    print(error)
}

But this does not work any more. I see the following error message:

Error Domain=io.realm Code=1 "open() failed: Operation not permitted" UserInfo={Error Code=1, NSLocalizedDescription=open() failed: Operation not permitted}

I don't understand why this doesn't work. I'm quite sure that the file is found in the bundle, so it seems like a problem of security?


Solution

  • You need to open the Realm as read-only if it resides within your application bundle as files within the application bundle are not writable. You were doing this in your old code snippet via readOnly: true, but you're not doing the equivalent in the new code snippet. You can see this being done in the example code in the Other Realms section of the Realm documentation.