Unable to open area along path

I am trying to use a batch file in real time without success. I know that my real-time file was copied to the application directory, but I did not read it.

fatal error: 'try!' the expression unexpectedly caused an error: "Could not open the region on the path '/Users/.../Library/Developer/CoreSimulator/Devices/.../data/Containers/Data/Application/.../Documents/default-v1. realm: Use the path in which your application has read and 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 the default configuration setting used for Realm:

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

Realm.Configuration.defaultConfiguration = config

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

, , readOnly true Realm.Configuration. , - Realm, - .... Library.

Realm 0.97.0
Xcode Version 7.1.1
+4
3

realm, Realm, . : Write only (Dropbox). , read/write setAttributes. :

 // rw rw r : Attention for octal-literal in Swift "0o".
 let permission = NSNumber(short: 0o664)

  do {
    try fileManager.setAttributes([NSFilePosixPermissions:permission], ofItemAtPath: fileInDocuments)

  } catch { print(error) }

.

0

, - ( Realm GitHub).

, , - ( ), .

, , .

  • / , fileInDocuments, fileInBundle , .

  • SimPholders , Realm , .

  • Realm , Realm , Realm, , - .

  • iOS, , .

  • , Realm Realm ( default.realm Documents), ,

, , , .:)

0

, , , , Realm , , .

, Realm : https://realm.io/docs/objc/latest/api/Enums/RLMError.html

, , Realm , Realm RLMErrorFilePermissionDenied. RLMErrorFileNotFound.

, , RLMErrorFileAccess, ...

0
source

Source: https://habr.com/ru/post/1623970/


All Articles