I want to copy preloaded realm file, so I do:
Copied my file to project navigator named default.realm
. Then check if fileExists:
:
let bundlePath = Bundle.main.path(forResource: "default", ofType: "realm")
let destPath = Realm.Configuration.defaultConfiguration.fileURL?.path
let fileManager = FileManager.default
if fileManager.fileExists(atPath: destPath!) {
//File exist, do nothing
print("File exist")
} else {
do {
//Copy file from bundle to Realm default path
try fileManager.copyItem(atPath: bundlePath!, toPath: destPath!)
print("Copied")
} catch {
print("\n",error)
}
}
And it always returns true, so I can't copy my realm file.
I ran your code in Simulator after resetting the contents and settings (
Simulator > Reset Content and Settings...
) and it appeared to execute as intended.If you've previously used Realm in your project (i.e. testing), it's probably seeing the old
default.realm
file.