CoreData Swift:崩溃:SQLite错误代码:14,'无法打开数据库文件'

时间:2015-09-22 22:05:38

标签: ios swift sqlite core-data

我在Xcode 7上使用swift 2.0。 我想在CoreData中存储JSON文件中的名称。

我成功在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'}]

编辑:

我发现了这个page,我不知道它是否有帮助。 (旧)

1 个答案:

答案 0 :(得分:1)

我认为问题可能是因为您的save语句每次保存新人时都会创建应用程序委托的新实例

try AppDelegate().managedObjectContext.save()

尝试使用您之前使用的相同的现有实例:

try managedContext.save()