CoreData Swift:崩溃:SQLite 错误 code:14、'unable to open database file'
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'}]
编辑:
我找到了这个page,不知道有没有帮助。 (旧)
我认为问题可能是因为你的save
语句正在创建应用程序委托的新实例,每次你保存一个新人:
try AppDelegate().managedObjectContext.save()
尝试使用您之前使用的相同的现有实例:
try managedContext.save()
我正在使用 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'}]
编辑:
我找到了这个page,不知道有没有帮助。 (旧)
我认为问题可能是因为你的save
语句正在创建应用程序委托的新实例,每次你保存一个新人:
try AppDelegate().managedObjectContext.save()
尝试使用您之前使用的相同的现有实例:
try managedContext.save()