Realm fileExists is always true

2019-03-03 09:26发布

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.

标签: swift3 realm
1条回答
手持菜刀,她持情操
2楼-- · 2019-03-03 10:00

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.

查看更多
登录 后发表回答