I’ve an app which makes use of realmSwift Database.
I am attempting to manually save the database to icloud which works positive to this point Right here is my code:
Backup:
let fileManager = FileManager.default
non-public func retrieveLocalRealmURL() -> URL {
let urls = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
let documentaryDirectory = urls[0]
let realmURL = documentaryDirectory.appendingPathComponent("default.realm");
return realmURL
}
non-public func backupRealmToiCloudDrive() {
let backgroundQueue = DispatchQueue.world(qos: .background)
backgroundQueue.async {
guard
let ubiquityURL = FileManager.default.url(forUbiquityContainerIdentifier: nil)
else {
return
}
let iCloudDriveURL = ubiquityURL.appendingPathComponent("Paperwork")
let iCloudRealmURL = iCloudDriveURL.appendingPathComponent("default.realm")
let fileExists = FileManager.default.fileExists(atPath: iCloudDriveURL.path, isDirectory: nil)
func copy() {
let localRealmURL = self.retrieveLocalRealmURL()
do {
attempt FileManager.default.copyItem(at: localRealmURL, to: iCloudRealmURL)
DispatchQueue.major.async {
let alert = UIAlertController(title: "Sicherung erfolgreich", message: "Die Datenbank wurde erfolgreich in iCloud wiederhergestellt.", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", model: .default, handler: nil))
self.current(alert, animated: true, completion: nil)
}
print("Realm File succesfully uploaded to iCloud")
} catch {
print(error.localizedDescription)
}
}
if fileExists {
self.deleteExistedFile(iCloudRealmURL)
copy()
print("File already existts, deleting outdated copy")
} else {
do {
attempt FileManager.default.createDirectory(at: iCloudDriveURL, withIntermediateDirectories: true, attributes: nil)
copy()
} catch {
print(error.localizedDescription)
}
}
}
}
non-public func deleteExistedFile(_ url: URL) {
let fileCoordinator = NSFileCoordinator(filePresenter: nil)
fileCoordinator.coordinate(writingItemAt: url, choices: .forDeleting, error: nil) { deleteURL in
do {
let fileExists = FileManager.default.fileExists(atPath: deleteURL.path, isDirectory: nil)
if fileExists {
attempt FileManager.default.removeItem(at: deleteURL)
}
} catch {
print(error.localizedDescription)
}
}
}
And right here is my code to revive from iCloud:
non-public func restoreRealmFromiCloudDrive() {
let backgroundQueue = DispatchQueue.world(qos: .background)
backgroundQueue.async {
guard let ubiquityURL = FileManager.default.url(forUbiquityContainerIdentifier: nil) else {
return
}
let iCloudDriveURL = ubiquityURL.appendingPathComponent("Paperwork")
let iCloudRealmURL = iCloudDriveURL.appendingPathComponent("default.realm")
let fileExists = FileManager.default.fileExists(atPath: iCloudRealmURL.path, isDirectory: nil)
if fileExists {
let defaultRealmFileURL = Realm.Configuration.defaultConfiguration.fileURL
let localRealmURL = self.retrieveLocalRealmURL()
let fileCoordinator = NSFileCoordinator(filePresenter: nil)
fileCoordinator.coordinate(writingItemAt: localRealmURL, choices: .forReplacing, error: nil) { writeURL in
do {
print("Realm File exists on iCloud")
if FileManager.default.fileExists(atPath: defaultRealmFileURL!.path) {
attempt FileManager.default.removeItem(at: defaultRealmFileURL!)
}
attempt FileManager.default.copyItem(at: iCloudRealmURL, to: defaultRealmFileURL!)
DispatchQueue.major.async {
let alert = UIAlertController(title: "Restore Profitable", message: "The restore from iCloud Drive was profitable", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", model: .default, handler: nil))
self.current(alert, animated: true, completion: nil)
}
} catch {
print(error.localizedDescription)
print("No Realm File on iCloud")
}
}
}
}
}
The file is succesfully written to iCloud, and I can even restore from there. Nonetheless, after I delete the app and reinstall it, the restore operate does not do something anymore. Not even print statemenents are seen within the console. Can somebody assist me out with this?
Restore also needs to work after a reinstall of the app.