Swift Realm, load the pre-populated database the right way?

后端 未结 3 578
情歌与酒
情歌与酒 2020-12-31 19:21

I\'m pretty new to ios development.

I follow this migration example to use pre-populated database and change the code a little bit

here is the final code I u

3条回答
  •  说谎
    说谎 (楼主)
    2020-12-31 19:54

    In Swift 3.0, try this:

        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(fileManager.fileExists(atPath: destPath!))
        } else {
            do {
                //Copy file from bundle to Realm default path
                try fileManager.copyItem(atPath: bundlePath!, toPath: destPath!)
            } catch {
                print("\n",error)
            }
        }
    

提交回复
热议问题