Skip to content

Instantly share code, notes, and snippets.

@TimOliver
Last active February 28, 2017 05:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TimOliver/79aaafa269865652e1da03cb384851d5 to your computer and use it in GitHub Desktop.
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
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