Created
September 21, 2020 05:19
-
-
Save imnaveensharma/1c39bcac7d702f14d87292af01eb0aa7 to your computer and use it in GitHub Desktop.
Update/Change default realm database location and prevent database file from sharing iTunes(files app) and iCloud
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func setupRealmConfiguration() { | |
var realmConfig = Realm.Configuration.defaultConfiguration | |
let paths = NSSearchPathForDirectoriesInDomains(.applicationSupportDirectory, | |
.userDomainMask, | |
true) | |
let applicationSupportDirectoryPath = paths.first | |
if let applicationSupportDirectoryPath = applicationSupportDirectoryPath { | |
let realmPath = applicationSupportDirectoryPath.appending("/default.realm") | |
realmConfig.fileURL = URL(fileURLWithPath: realmPath) | |
Realm.Configuration.defaultConfiguration = realmConfig | |
} | |
//Remove files from document directory, to avoid sharing iTunes(files app) and iCloud | |
let oldPaths = NSSearchPathForDirectoriesInDomains(.applicationSupportDirectory, | |
.userDomainMask, | |
true) | |
do { | |
try FileManager.default.removeItem(atPath: oldPaths[0].appending("/default.realm")) | |
try FileManager.default.removeItem(atPath: oldPaths[0].appending("/default.realm.lock")) | |
try FileManager.default.removeItem(atPath: oldPaths[0].appending("/default.realm.management")) | |
} catch { | |
dLog(message: "Handle error received") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment