2014-12-02 154 views
0

什麼需要修改來預加載我的sqlite文件?我將該文件添加到項目中,這讓我認爲我必須對此代碼進行更改。Swift核心數據預加載persistentStoreCoordinator:

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 
}() 
+0

您是否介意發佈您在Swift上找到本教程的位置?我一直在尋找這樣的一些時間。 – martin 2015-02-28 10:49:51

+0

我用來達到這一點的youtube視頻不再可用。 – MwcsMac 2015-02-28 16:06:12

回答

1

只需將文件url更改爲指向您的SQLite文件。

您從包到文檔目錄需要

  1. 副本SQLite的文件。
  2. 參考文件網址addPersistentStore...

例如

// 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) 
+0

請提供一個例子。 – MwcsMac 2014-12-03 20:55:08

+0

你有什麼嘗試?檢查[NSFileManager](https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSFileManager_Class/index.html)和[NSBundle](https://developer.apple.com/庫/ IOS /文檔/可可/參考/基金/班/ NSBundle_Class/index.html的)。 – Mundi 2014-12-03 21:19:24

+0

NSFileManager.defaultManager()。copyItemAtPath(path,toPath:destinationPath,error:nil)我得到'NSURL'不是'NSString'的子類型 – MwcsMac 2014-12-04 20:44:23

0

這是爲我工作的最終代碼。請注意該部分//複製並記住,在運行此設置之前,您必須首先從設備或模擬器中刪除應用程序。

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 
}()