My iOS app is using Realm database I want to copy or move the current default.realm database file to the new directory (App Group location) so that I can share it with Today Extension widget.
I tried as this post says (How to transfer Realm database to appgroups)
The core code is
let fileManager = FileManager.default
let originalPath = Realm.Configuration.defaultConfiguration.fileURL!
let appGroupURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.com.myApp")!.appendingPathComponent("default.realm")
do{
try fileManager.replaceItemAt(appGroupURL, withItemAt: originalPath)
}
catch{
print("Error information: \(error)")
}
And I put this code inside Realm migration scope inside didFinishLaunchingWithOptions as below To give you clearer picture where I'm using this code.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
var config = Realm.Configuration(
schemaVersion: 1,
migrationBlock: { migration, oldSchemaVersion in
if (oldSchemaVersion < 1) {
let fileManager = FileManager.default
let originalPath = Realm.Configuration.defaultConfiguration.fileURL!
let appGroupURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.com.myApp")!.appendingPathComponent("default.realm")
do{
try fileManager.replaceItemAt(appGroupURL, withItemAt: originalPath)
}
catch{
print("Error information: \(error)")
}
}
}
)
}
When I try this, my console says the file couldn't be saved.
Error Domain=NSCocoaErrorDomain Code=512 "The file “default.realm” couldn’t be saved in the folder “2EEADCEE-F9D9-44A8-BDED-B60A689656A2”." UserInfo={NSFileOriginalItemLocationKey=file:///Users/jm/Library/Developer/CoreSimulator/Devices/38334AE3-6648-402E-AC18-8252426002D6/data/Containers/Shared/AppGroup/2EEADCEE-F9D9-44A8-BDED-B60A689656A2/default.realm, ......
I heard copy / moving Realm database file should be done before opening the file, is this error related to that?
Thanks for your help and have a great day
didFinishLaunchingWithOptions
then it won't work. 2) This may be a sandboxing issue, does your app have access to the folder being written to? – Jay