So I have one iOS app that uses Realm database and I've been implementing new stuff with SiriKit so now I am using App Groups that are working well for NSUserDefault.
But now how can I migrate the Realm database which is the default location to a shared folder so that the Siri extension has access to these values?
This is the code that I'm going to use to read realm in the extension
let fileURL = FileManager.default
.containerURL(forSecurityApplicationGroupIdentifier: "group.com.x")!
.appendingPathComponent("default.realm")
let config = Realm.Configuration(
fileURL: fileURL,
objectTypes: [x1.self, x2.self, EquipmentConsumptionDay.self])
_ = try! Realm(configuration: config)
The solution is easy you just need to copy realm file to the new location.
// Migrate now the DATABASE realm
// Copy the file from the default location to the new real location
let fileManager = FileManager.default
let originalPath = Realm.Configuration.defaultConfiguration.fileURL!
let appGroupURL = FileManager.default
.containerURL(forSecurityApplicationGroupIdentifier: "group.com.xx.siri")!
.appendingPathComponent("default.realm")
do{
try _ = fileManager.replaceItemAt(appGroupURL, withItemAt: originalPath)
}
catch{
print("Error info: \(error)")
}
Don't forget to make system for migration so this code just run once