Swift核心数据预加载persistentStoreCoordinator:

时间:2014-12-02 19:34:16

标签: core-data swift xcode6.1

需要修改哪些内容才能预加载我的sqlite文件?我将文件添加到项目中,这使我认为我必须在此代码中进行更改。

    lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator? = {
    // The persistent store coordinator for the application. This implementation creates and return a coordinator, having added 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.
    // Create the coordinator and store
    var coordinator: NSPersistentStoreCoordinator? = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel)
    let url = self.applicationDocumentsDirectory.URLByAppendingPathComponent("junkapp.sqlite")
    var error: NSError? = nil
    var failureReason = "There was an error creating or loading the application's saved data."
    if coordinator!.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: url, options: nil, error: &error) == nil {
        coordinator = nil
        // Report any error we got.
        let dict = NSMutableDictionary()
        dict[NSLocalizedDescriptionKey] = "Failed to initialize the application's saved data"
        dict[NSLocalizedFailureReasonErrorKey] = failureReason
        dict[NSUnderlyingErrorKey] = error
        //error = NSError.errorWithDomain("YOUR_ERROR_DOMAIN", code: 9999, userInfo: dict)
        // Replace this with code to handle the error appropriately.
        // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
        NSLog("Unresolved error \(error), \(error!.userInfo)")
        abort()
    }

    return coordinator
}()

2 个答案:

答案 0 :(得分:1)

只需更改文件网址即可指向您的SQLite文件。

你需要

  1. 将捆绑包中的SQLite文件复制到文档目录。
  2. addPersistentStore...
  3. 中引用此文件网址

    e.g。

    // Copying
    let path = NSBundle.mainBundle().pathForResource("sqlitefile", ofType:"sqlite")!
    let destinationPath = 
      self.applicationDocumentsDirectory.URLByAppendingPathComponent("junkapp.sqlite")!.path
    NSFileManager.defaultManager().copyItemAtPath(
      path, toPath: destinationPath, error:nil)
    
    // Using
    coordinator!.addPersistentStoreWithType(NSSQLiteStoreType, 
      configuration: nil, URL: NSURL.fileURLWithPath(destinationPath), 
      options: nil, error: &error)
    

答案 1 :(得分:1)

这是对我有用的最终代码。请注意部分//复制并记住,在运行此设备之前,您必须先删除设备或模拟器上的应用程序。

lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator? = {
    // The persistent store coordinator for the application. This implementation creates and return a coordinator, having added 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.
    // Create the coordinator and store
    var coordinator: NSPersistentStoreCoordinator? = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel)
    let url = self.applicationDocumentsDirectory.URLByAppendingPathComponent("junkapp.sqlite")
    var error: NSError? = nil
    var failureReason = "There was an error creating or loading the application's saved data."
    // Copying
    let path = NSBundle.mainBundle().pathForResource("junkapp", ofType:"sqlite")!
    NSFileManager.defaultManager().copyItemAtPath(path, toPath: url.path!, error:nil)
    //end copy
    if coordinator!.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: NSURL.fileURLWithPath(url.path!), options: nil, error: &error) == nil {
        coordinator = nil
        // Report any error we got.
        let dict = NSMutableDictionary()
        dict[NSLocalizedDescriptionKey] = "Failed to initialize the application's saved data"
        dict[NSLocalizedFailureReasonErrorKey] = failureReason
        dict[NSUnderlyingErrorKey] = error
        //error = NSError.errorWithDomain("YOUR_ERROR_DOMAIN", code: 9999, userInfo: dict)
        // Replace this with code to handle the error appropriately.
        // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
        NSLog("Unresolved error \(error), \(error!.userInfo)")
        abort()
    }

    return coordinator
}()