2014-12-02 4 views
0

sqlite 파일을 미리로드하려면 무엇을 수정해야합니까? 프로젝트에 파일을 추가 했으므로이 코드에서 변경해야한다고 생각합니다.Swift Core 데이터 미리로드 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

신속한 튜토리얼을 어디에서 찾을 수 있습니까? 이런 식으로 꽤 오랫동안 찾고있었습니다. – martin

+0

지금까지 사용하던 YouTube 비디오를 더 이상 사용할 수 없습니다. – MwcsMac

답변

1

파일 url이 SQLite 파일을 가리 키도록 변경하십시오.

당신은 문서 디렉토리에 번들에서

  1. 복사 할 SQLite는 파일이 필요합니다.
  2. 이 파일의 URL은 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

+0

무엇을 시도 했습니까? [NSFileManager] (https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSFileManager_Class/index.html)와 [NSBundle] (https://developer.apple.com/)을 확인하십시오. 라이브러리/ios/documentation/Cocoa/Reference/Foundation/Classes/NSBundle_Class/index.html). – Mundi

+0

NSFileManager.defaultManager(). copyItemAtPath (path, toPath : destinationPath, error : nil) 'NSURL'이 'NSString'의 하위 유형이 아닙니다. – MwcsMac

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