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

后端 未结 3 598
情歌与酒
情歌与酒 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:51

    try this

        let realm_db_path     = Realm.Configuration.defaultConfiguration.fileURL!
        let bundle_realm_path = Bundle.main.url(forResource: "default", withExtension: "realm")!
    
        if !FileManager.default.fileExists(atPath: realm_db_path.absoluteString){
            do {
    
                try FileManager.default.copyItem(at: bundle_realm_path, to: realm_db_path)
    
            }catch let error {
    
                NSLog(error as! String)
    
            }
    

提交回复
热议问题