views:

20

answers:

0

Hi All,

I am working on a chat application and using coredata to store the names of the person from which you have started chat to make it persists during application launches....

The problem is sometimes my sqlite db gets corrupted..The error that I get is:-

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';
}

To isolate the issue I commented out all the code to write values in db..and only enabled code to read [fetchRequest] from coredata…

Now what I see is when I install the app for the first time..I can see sql executing table creation commands and stuff [on the console]. At this point if I retrieve the sqlite file and inspect it with Sqlite Database Browser, I can see the tables that were created…

Now as soon as I move forward and execute a fetch request on that [though there will be no records at this time because its a newly created database]….it works without error.. Now if I close the app and relaunch it again I get the following error EITHER while opening the persistence store…

                    OR

Sometimes it will open and fetching will be successful but then on closing the app and relaunching it again the sqlite.db will get corrupted..

            -------------------------------------

This doesn't happen on the simulator so I am assuming the implementation the logic to access and modify the records is correct…But there is something I am missing on the device while handling the database…May be I am not closing the context,model and persistence store..I tried that too BUT even that didn't work. Any hints? I am also posting the code at the bottom

============== persistent store creation/opening ===================

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
}    

================ fetch request ======================

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");

}

------------- NOW I AM GETTING THIS --------------- Now I am getting the following error..The scenario is...Open a persistence store ....read from it....close the app. Now when u will again launch the app you will get:-

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;}

This happens immediately sometime OR sometime do the process 2-3 times and it will happen