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)
, , readOnly true Realm.Configuration. , - Realm, - .... Library.
Realm 0.97.0
Xcode Version 7.1.1