Skip to content

Instantly share code, notes, and snippets.

@imnaveensharma
Created September 21, 2020 05:19
Show Gist options
  • Save imnaveensharma/1c39bcac7d702f14d87292af01eb0aa7 to your computer and use it in GitHub Desktop.
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
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