具有预填充.sqlite(Swift3)

时间:2016-09-02 06:26:53

标签: swift sqlite core-data swift3 ios10

目前我正在研究现有iOS9应用程序的Swift3 / iOS10更新,该应用程序为欧洲各地的电动汽车存储约10,000个充电点。到目前为止,我总是使用预先填充的数据库(.xcappdata包中的.sqlite,.sqlite-shm,.sqlite-wal文件)发送应用程序,但是使用当前版本的Apple正在引入NSPersistentContainer类,这使它有点复杂。在我的AppDelegate类中,我正在实例化我的NSPersistentContainer对象并将其传递给一个懒惰的var,就像Apple在每个示例代码中所做的那样:

   lazy var stationDataPersistentContainer: NSPersistentContainer = {  
     let fileMgr = FileManager.default
     let destinationModel = NSPersistentContainer.defaultDirectoryURL()
     if !fileMgr.fileExists(atPath: destinationModel.appendingPathComponent("StationData.sqlite").path) {
         do {
             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 {
         //
     }
     /* 
     The persistent container for the application. This implementation 
     creates and returns a container, having loaded the store for the 
     application to it. This property is optional since there are legitimate 
     error conditions that could cause the creation of the store to fail. 
     */
    let container = NSPersistentContainer(name: "StationData")  
    container.loadPersistentStores(completionHandler: { (storeDescription, error) in  
         if let error = error as NSError? {  
             /* 
              * Typical reasons for an error here include: 
              * The parent directory does not exist, cannot be created, or disallows writing. 
              * The persistent store is not accessible, due to permissions or data protection when the device is locked. 
              * The device is out of space. 
              * The store could not be migrated to the current model version. 
              * Check the error message to determine what the actual problem was. 
              */  
             fatalError("Unresolved error \(error), \(error.userInfo)")  
        }
    })  
    return container  
    }()

在iOS9版本中,我将文件复制到适当的目录,就像您在以下代码示例中看到的那样:

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!) {  
         do {  
              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 {  
              //  
         } do {  
              try coordinator.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: url,  
                                                       options: [NSMigratePersistentStoresAutomaticallyOption:true, NSInferMappingModelAutomaticallyOption:true])  
         } catch {  
              //  
         }  
    } else {  
         //  
    }  
    return coordinator
}() 

有几天我试图将文件移动到NSPersistentContainer.defaultDirectoryURL() -> URL返回的正确目录,但是每当我收到错误时,该文件已经存在,因为我的stationDataPersistentContainer已经初始化,所以NSPersistentContainer有足够的时间来生成sqlite *文件。即使我尝试复制文件并在覆盖的init()函数中初始化stationDataPersistentContainer,我也无法做到这一点。在文档中有什么我遗漏或忽略的吗?哪个是将安装应用程序的现有数据复制到coredata的最佳/正确/适当方式。

附录

仅为了您的信息,我还可以存储JSON文件,我从API获取到Documents目录并运行JSON解析器,但这需要大量的资源,特别是时间! (此问题也发布在Apple开发者论坛上并等待批准)

2 个答案:

答案 0 :(得分:5)

我就是这样做的:

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


    }()

答案 1 :(得分:3)

最简单的解决方案是不使用NSPersistentContainer - 它只是方便取走堆栈样板,如果你正在更新已经在运行的应用程序,你可以保留你的代码。

如果要迁移到NSPersistentContainer,请在调用loadPersistentStores之前移动文件 - 此时首先创建SQL文件。

请注意,NSPersistentContainer可能没有使用文档目录 - 在iOS模拟器上开箱即用它使用应用程序支持目录。如果使用documents目录对您很重要,那么您必须继承NSPersistentContainer并覆盖defaultDirectoryURL类方法,或者向NSPersistentContainer提供NSPersistentStoreDescription,告知它存储数据的位置。