【问题标题】:CoreData Swift : Crash : SQLite error code:14, 'unable to open database file'CoreData Swift:崩溃:SQLite 错误代码:14,“无法打开数据库文件”
【发布时间】:2015-09-22 22:05:37
【问题描述】:

我正在使用带有 Xcode 7 的 swift 2.0。 我想将 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'}]

编辑:

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

【问题讨论】:

    标签: ios swift sqlite core-data


    【解决方案1】:

    我认为问题可能是因为您的save 语句正在创建应用程序委托的新实例,每次您保存一个新人时:

    try AppDelegate().managedObjectContext.save()
    

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

    try managedContext.save()
    

    【讨论】:

      猜你喜欢
      • 2015-02-16
      • 2020-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-29
      • 1970-01-01
      • 2020-09-13
      相关资源
      最近更新 更多