我正在开发一个聊天应用程序,并使用coredata存储您已开始聊天的人的姓名,以便在应用程序启动期间保持该状态....
问题是有时候我的sqlite数据库被破坏了......我得到的错误是: -
Unresolved error Error Domain=NSCocoaErrorDomain Code=259 UserInfo=0x2d3080 "Operation
could not be completed. (Cocoa error 259.)",
{
NSFilePath = "/var/mobile/Applications/23FFAFDA-98BC-4519-AB09-
3B9BA05D667C/Documents/QuickChat/QuickChat.sqlite";NSUnderlyingException = Fatal error. The database at
/var/mobile/Applications/23FFAFDA-
98BC-4519-AB09-3B9BA05D667C/Documents/QuickChat/QuickChat.sqlite is corrupted.
SQLite error code:26, 'file is encrypted or is not a database';
}
为了隔离这个问题,我注释掉了所有在db..中写入值的代码,并且只启用了从coredata读取[fetchRequest]的代码...
现在我看到的是第一次安装应用程序时..我可以看到sql执行表创建命令和[在控制台上]的东西。此时,如果我检索sqlite文件并使用Sqlite数据库浏览器检查它,我可以看到创建的表...
现在,一旦我继续前进并执行一个获取请求[虽然此时没有记录,因为它是一个新创建的数据库] ...。它可以正常工作.. 现在,如果我关闭应用程序并重新启动它,我会在打开持久性存储时遇到以下错误......
OR
有时它会打开并且提取会成功,但随后关闭应用程序并再次重新启动sqlite.db将会损坏..
-------------------------------------
这在模拟器上不会发生,所以我假设实现访问和修改记录的逻辑是正确的...但是在处理数据库时我在设备上缺少某些东西...可能是我没有关闭上下文,模型和持久性存储.I 尝试过,但即使这样也行不通。任何提示?我也在底部发布代码
==============持久存储创建/开放===================
NSURL *storeUrl = [NSURL fileURLWithPath:storePath];
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];
persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: [self managedObjectModel]];
NSError *error;
if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:options error:&error]) {
// Update to handle the error appropriately.
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
///////////////////////////////////////////////////////////////////
[[NSFileManager defaultManager] removeItemAtPath:storeUrl.path error:&error];
///////////////////////////////////////////////////////////////////
exit(-1); // Fail
}
================获取请求======================
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"QuickChatList" inManagedObjectContext:self.managedObjectContext];
[request setEntity:entity];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"creation" ascending:NO];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[request setSortDescriptors:sortDescriptors];
[sortDescriptor release];
[sortDescriptors release];
NSPredicate* predicate = [NSPredicate predicateWithFormat:@"loginName like %@",uname];
[request setPredicate:predicate];
// Execute the fetch -- create a mutable copy of the result.
NSError *error = nil;
NSMutableArray *mutableFetchResults = [[self.managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
if (mutableFetchResults == nil) {
// Handle the error.
NSLog(@" ERROR IN RETREIVING CHAT LIST");
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
}
else {
NSLog(@" CHAT LIST RETREIVE SUCESSFULL");
}
-------------我现在得到这个--------------- 现在我收到以下错误..场景是......打开持久性存储....从中读取....关闭应用程序。现在当你再次启动应用程序时,你会得到: -
unknown QuickChat[643] <Warning>: Unresolved error Error Domain=NSCocoaErrorDomain Code=259 UserInfo=0x235bb0 "Operation could not be completed. (Cocoa error 259.)", {
NSFilePath = "/var/mobile/Applications/FBD15983-D47B-4F63-BEEE-A7CC18460ACF/Documents/QuickChat/QuickChat.sqlite";
NSUnderlyingException = File at path does not appear to be a SQLite database: /var/mobile/Applications/FBD15983-D47B-4F63-BEEE-A7CC18460ACF/Documents/QuickChat/QuickChat.sqlite;}
这种情况会在某个时间立即发生,或者某个时间进行2-3次并且会发生