Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to open a realm at path

Tags:

ios

swift

realm

I am trying to use a bundled realm file without success. I know that my realm file was copied successfully to my application’s Directory but I ca not read it.

fatal error: 'try!' expression unexpectedly raised an error: "Unable to open a realm at path '/Users/…/Library/Developer/CoreSimulator/Devices/…/data/Containers/Data/Application/…/Documents/default-v1.realm'. Please use a path where your app has read-write permissions."

 func fullPathToFileInAppDocumentsDir(fileName: String) -> String {

        let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory,NSSearchPathDomainMask.UserDomainMask,true)

        let documentsDirectory = paths[0] as NSString

        let fullPathToTheFile = documentsDirectory.stringByAppendingPathComponent(fileName)

        return fullPathToTheFile
    }

In didFinishLaunchingWithOptions:

  let fileInDocuments = fullPathToFileInAppDocumentsDir("default-v1.realm")

        if !NSFileManager.defaultManager().fileExistsAtPath(fileInDocuments) {

            let bundle = NSBundle.mainBundle()

            let fileInBundle = bundle.pathForResource("default-v1", ofType: "realm")

            let fileManager = NSFileManager.defaultManager()

            do {
                try fileManager.copyItemAtPath(fileInBundle!, toPath: fileInDocuments)
            } catch {  print(error)  }
        }

And setting the configuration used for the default Realm:

var config = Realm.Configuration()
config.path = fileInDocuments

Realm.Configuration.defaultConfiguration = config

let realm = try! Realm(configuration: config)  // It fails here!!! :-)

As the documentation suggests, I have tried as well to open it directly from the bundle path by setting readOnly to true on the Realm.Configuration object. I am not sure if this is something related to Realm or if I am overlooking something with the file system…. I have also tried to store the file in the Library folder.

Realm 0.97.0
Xcode Version 7.1.1
like image 384
Fabio Avatar asked Oct 19 '22 18:10

Fabio


1 Answers

That exception gets thrown whenever a low level I/O operation is denied permission to the file you've specified (You can check it out on Realm's GitHub account).

Even though everything seems correct in your sample code there, something must be set incorrectly with the file location (Whether it be the file path to your bundle's Realm file, or the destination path) to be causing that error.

A couple of things I can recommend trying out.

  • Through breakpoints/logging, manually double-check that both fileInDocuments and fileInBundle are being correctly created and are pointing at the locations you were expecting.

  • When running the app in the Simulator, use a tool like SimPholders to track down the Documents directory of your app on your computer, and visually ensure that the Realm file is being properly copied to where you're expecting.

  • If the Realm file is in the right place, you can also Realm's Browser app to try opening the Realm file to ensure that the file was copied correctly and still opens properly.

  • Try testing the code on a proper iOS device to see if the same error is occurring on the native system.

  • If all else fails, try doing some Realm operations with the default Realm (Which will simply deposit a default.realm file in your Documents directory), just to completely discount there isn't something wrong with your file system

Let me know how you go with that, and if you still can't work out the problem, we can keep looking. :)

like image 99
TiM Avatar answered Oct 21 '22 15:10

TiM