Last active
February 28, 2017 05:24
-
-
Save TimOliver/79aaafa269865652e1da03cb384851d5 to your computer and use it in GitHub Desktop.
A quick code snippet showing how to make an already-created Realm file private
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
let fileManager = FileManager.default | |
var configuration = Realm.Configuration() | |
let defaultRealmURL = configuration.fileURL! | |
// Get the path to the Library directory and append 'default.realm' to it | |
let libraryDirectoryPath = NSSearchPathForDirectoriesInDomains(.libraryDirectory, .userDomainMask, true).first! | |
let libraryDirectoryURL = URL(fileURLWithPath: libraryDirectoryPath) | |
let newRealmURL = libraryDirectoryURL.appendingPathComponent(defaultRealmURL.lastPathComponent) | |
// There is a user-created default.realm in the Documents directory | |
if fileManager.fileExists(atPath: defaultRealmURL.path) { | |
try! fileManager.moveItem(atPath: defaultRealmURL.path, toPath: newRealmURL.path) | |
} | |
// Set the Realm configuration to point to the new Realm file location | |
configuration.fileURL = newRealmURL | |
Realm.Configuration.defaultConfiguration = configuration | |
// calls to 'Realm()' will now come from the Library directory |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment