swift - Core Data with pre-filled .sqlite (Swift3) -


currently i'm working on swift3 / ios10 update of existing ios9 app stores 10.000 charging points electric vehicles across europe. shipped application pre-filled database (.sqlite, .sqlite-shm, .sqlite-wal files .xcappdata bundle), current version apple introducing nspersistentcontainer class, makes bit more complicated. in appdelegate class i'm instantiating nspersistentcontainer object , passing lazy var it's done apple in every example code:

   lazy var stationdatapersistentcontainer: nspersistentcontainer = {        let filemgr = filemanager.default      let destinationmodel = nspersistentcontainer.defaultdirectoryurl()      if !filemgr.fileexists(atpath: destinationmodel.appendingpathcomponent("stationdata.sqlite").path) {          {              try filemgr.copyitem(at: url(fileurlwithpath: bundle.main.resourcepath!.appending("/stationdata.sqlite")), to: destinationmodel.appendingpathcomponent("/stationdata.sqlite"))              try filemgr.copyitem(at: url(fileurlwithpath: bundle.main.resourcepath!.appending("/stationdata.sqlite-shm")), to: destinationmodel.appendingpathcomponent("/stationdata.sqlite-shm"))              try filemgr.copyitem(at: url(fileurlwithpath: bundle.main.resourcepath!.appending("/stationdata.sqlite-wal")), to: destinationmodel.appendingpathcomponent("/stationdata.sqlite-wal"))          } catch {              //          }      } else {          //      }      /*       persistent container application. implementation       creates , returns container, having loaded store       application it. property optional since there legitimate       error conditions cause creation of store fail.       */     let container = nspersistentcontainer(name: "stationdata")       container.loadpersistentstores(completionhandler: { (storedescription, error) in            if let error = error nserror? {                /*                * typical reasons error here include:                * parent directory not exist, cannot created, or disallows writing.                * persistent store not accessible, due permissions or data protection when device locked.                * device out of space.                * store not migrated current model version.                * check error message determine actual problem was.                */                fatalerror("unresolved error \(error), \(error.userinfo)")           }     })       return container       }() 

in ios9 version im copying files apropriate directory, can see in following code example:

lazy var persistentstorecoordinator: nspersistentstorecoordinator = {       let coordinator = nspersistentstorecoordinator(managedobjectmodel: self.managedobjectmodel)       let url = self.applicationdocumentsdirectory.urlbyappendingpathcomponent("stationdata.sqlite")       let filemgr = nsfilemanager.defaultmanager()       if !filemgr.fileexistsatpath(url.path!) {            {                 try filemgr.copyitematpath(nsbundle.mainbundle().pathforresource("stationdata", oftype: "sqlite")!, topath: self.applicationdocumentsdirectory.urlbyappment("stationdata.sqlite").path!)                 try filemgr.copyitematpath(nsbundle.mainbundle().pathforresource("stationdata", oftype: "sqlite-shm")!, topath: self.applicationdocumentsdirectory.urlbyappendingpathcomponent("stationdata.sqlite-shm").path!)                 try filemgr.copyitematpath(nsbundle.mainbundle().pathforresource("stationdata", oftype: "sqlite-wal")!, topath: self.applicationdocumentsdirectory.urlbyappendingpathcomponent("stationdata.sqlite-wal").path!)            } catch {                 //            } {                 try coordinator.addpersistentstorewithtype(nssqlitestoretype, configuration: nil, url: url,                                                          options: [nsmigratepersistentstoresautomaticallyoption:true, nsinfermappingmodelautomaticallyoption:true])            } catch {                 //            }       } else {            //       }       return coordinator }()  

for number of days have tried move files proper directory returned nspersistentcontainer.defaultdirectoryurl() -> url, everytime error, file exists because stationdatapersistentcontainer initialized , nspersistentcontainer had enough time generate sqlite* files. if try copy files , initialize stationdatapersistentcontainer in overwritten init() function not right. there i'm missing or overlooking in documentation? best/right/appropriate way copy existing data on installation of app coredata.

appendix:

just information, store json-files, api documents directory , run json-parser, needs lot of ressources , time! (this question posted on apple developers forum , waiting approval)

this how it:

lazy var persistentcontainer: nspersistentcontainer = {           let container = nspersistentcontainer(name: "app_name")          let seededdata: string = "app_name"         var persistentstoredescriptions: nspersistentstoredescription          let storeurl = self.applicationdocumentsdirectory.appendingpathcomponent("app_name.sqlite")          if !filemanager.default.fileexists(atpath: (storeurl.path)) {             let seededdataurl = bundle.main.url(forresource: seededdata, withextension: "sqlite")             try! filemanager.default.copyitem(at: seededdataurl!, to: storeurl)          }          print(storeurl)           container.persistentstoredescriptions = [nspersistentstoredescription(url: storeurl)]         container.loadpersistentstores(completionhandler: { (storedescription, error) in             if let error = error {                  fatalerror("unresolved error \(error),")             }         })           return container       }() 

Comments