【发布时间】: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