Skip to content

Instantly share code, notes, and snippets.

@anthonycastelli
Created August 12, 2016 21:25
Show Gist options
  • Save anthonycastelli/24d0717471b921eddc0051e60ab5c350 to your computer and use it in GitHub Desktop.
Save anthonycastelli/24d0717471b921eddc0051e60ab5c350 to your computer and use it in GitHub Desktop.
init() {
print("•", RLMRealm.default().configuration.fileURL?.path)
var config = Realm.Configuration(
// Set the new schema version. This must be greater than the previously used
// version (if you've never set a schema version before, the version is 0).
schemaVersion: 1,
// Set the block which will be called automatically when opening a Realm with
// a schema version lower than the one set above
migrationBlock: { migration, oldSchemaVersion in
// We haven’t migrated anything yet, so oldSchemaVersion == 0
if (oldSchemaVersion < 1) {
// Nothing to do!
// Realm will automatically detect new properties and removed properties
// And will update the schema on disk automatically
}
})
if let originalDefaultRealmPath = config.fileURL?.path {
let fileManager = FileManager.default
let directory = fileManager.containerURL(forSecurityApplicationGroupIdentifier: "group.Shyft")
if let realmPath = directory?.appendingPathComponent("default.realm").path {
print(realmPath)
if fileManager.fileExists(atPath: originalDefaultRealmPath) && !fileManager.fileExists(atPath: realmPath) {
do {
try fileManager.moveItem(atPath: originalDefaultRealmPath, toPath: realmPath)
config.fileURL = URL(fileURLWithPath: realmPath)
RLMRealm.default().configuration.fileURL = config.fileURL
} catch let error as NSError {
print("•", error)
}
}
}
}
// Tell Realm to use this new configuration object for the default Realm
Realm.Configuration.defaultConfiguration = config
print("•", RLMRealm.default().configuration.fileURL?.path)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment