簡體   English   中英

CoreData Swift:崩潰:SQLite錯誤代碼:14,“無法打開數據庫文件”

[英]CoreData Swift : Crash : SQLite error code:14, 'unable to open database file'

我將Swift 2.0與Xcode 7結合使用。我想將JSON文件中的名稱存儲在CoreData中。

我成功將數據保存到CoreData模型中,但是在值編號1272或1273之后,我的應用程序崩潰了。

這是代碼:

for (key,subJson):(String, JSON) in json {
                if let name = subJson["nom"].string {
                    print("key : \(key)")
                    self.savePerson(name)
                }
}

 func savePerson(name: String) {
    let appDelegate =
    UIApplication.sharedApplication().delegate as! AppDelegate
    let managedContext = appDelegate.managedObjectContext
    let entity =  NSEntityDescription.entityForName("Person",
        inManagedObjectContext:
        managedContext)
    let person = NSManagedObject(entity: entity!,
        insertIntoManagedObjectContext:managedContext)
    person.setValue(name, forKey: "name")
    do {
        try AppDelegate().managedObjectContext.save()
    } catch {
        fatalError("Failure to save context: \(error)")
    }
    people.append(person)
}

我試圖查看一個斷點,這是鍵1272或1273處的程序:

--> [savePerson()] try AppDelegate().managedObjectContext.save()
--> [AppDelegate] lazy var managedObjectContext: NSManagedObjectContext =
--> [AppDelegate][managedObjectContext] let coordinator = self.persistentStoreCoordinator

這就是崩潰! 我檢查了一下,名字確實存在,但不是零。 我試圖在代碼上睡一覺,但並非總是在同一時間。 您認為這是速度問題嗎?

其他,你知道為什么嗎?

日志是:

CoreData: error: (14) I/O error for database at     /.../.../.../SingleViewCoreData.sqlite.  SQLite error code:14, 'unable to open database file'
2015-09-22 23:59:59.774 testAlamofire2[8765:351886] CoreData: error: Encountered exception I/O error for database at /.../.../.../SingleViewCoreData.sqlite.  SQLite error code:14, 'unable to open database file' with userInfo {
NSFilePath = "/.../.../.../.../SingleViewCoreData.sqlite";
NSSQLiteErrorDomain = 14;
} while checking table name from store: <NSSQLiteConnection: 0x7f8e2d677ee0>
2015-09-22 23:59:59.779 testAlamofire2[8765:351886] CoreData: error: -addPersistentStoreWithType:SQLite configuration:(null) URL:file:///.../.../.../.../SingleViewCoreData.sqlite options:(null) ... returned error Error Domain=NSCocoaErrorDomain Code=256 "(null)" UserInfo={NSSQLiteErrorDomain=14, NSUnderlyingException=I/O error for database at .../.../.../SingleViewCoreData.sqlite.  SQLite error code:14, 'unable to open database file'} with userInfo dictionary {
NSSQLiteErrorDomain = 14;
NSUnderlyingException = "I/O error for database at /.../.../...SingleViewCoreData.sqlite.  SQLite error code:14, 'unable to open database file'";
}
2015-09-22 23:59:59.780 testAlamofire2[8765:351886] Unresolved error Error Domain=YOUR_ERROR_DOMAIN Code=9999 "Failed to initialize the application's saved data" UserInfo={NSLocalizedDescription=Failed to initialize the application's saved data, NSLocalizedFailureReason=There was an error creating or loading the application's saved data., NSUnderlyingError=0x7f8e2d6772d0 {Error Domain=NSCocoaErrorDomain Code=256 "(null)" UserInfo={NSSQLiteErrorDomain=14, NSUnderlyingException=I/O error for database at /.../.../.../SingleViewCoreData.sqlite.  SQLite error code:14, 'unable to open database file'}}}, [NSLocalizedDescription: Failed to initialize the application's saved data, NSLocalizedFailureReason: There was an error creating or loading the application's saved data., NSUnderlyingError: Error Domain=NSCocoaErrorDomain Code=256 "(null)" UserInfo={NSSQLiteErrorDomain=14, NSUnderlyingException=I/O error for database at /.../.../.../.../SingleViewCoreData.sqlite.  SQLite error code:14, 'unable to open database file'}]

編輯:

我找到此頁面 ,不知道是否有幫助。 (舊)

我認為問題可能是因為每次保存新人員時,您的save語句都會創建應用程序委托的新實例

try AppDelegate().managedObjectContext.save()

嘗試使用與先前使用的相同的現有實例:

try managedContext.save()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM