You can use Realm.Configuration.fileURL
to change a Realm file path. Like the following:
let cachesDirectoryPath = NSSearchPathForDirectoriesInDomains(.CachesDirectory, .UserDomainMask, true)[0]
let cachesDirectoryURL = NSURL(fileURLWithPath: cachesDirectoryPath)
let fileURL = cachesDirectoryURL.URLByAppendingPathComponent("Default.realm")
let config = Realm.Configuration(fileURL: fileURL)
let realm = try! Realm(configuration: config)
If you would not like to specify fileURL
every instantiating Realm, you can use Realm.Configuration.defaultConfiguration
. If you set a configuration object to defaultConfiguration
, Realm()
uses the configuration as default.
Realm.Configuration.defaultConfiguration = config
let realm = Realm()
See also... https://realm.io/docs/swift/latest/#realm-configuration