views:

39

answers:

1

Hello,

My app is crashing when testing on the device, but not on the simulator. It happens when I go to log out. I'm deleting all records from core data when the user logs out, code as follows:

-(IBAction)logOut

{
    UIAlertView *getConfirmation = [[UIAlertView alloc] initWithTitle:@"Confirm" message:@"Are you sure you want to logout. You will lose any unsync'ed workouts." delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Logout", nil];
    [getConfirmation show]; 
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 1){
        // Clear the database of all objects when the user logs out.
        [self deleteAllObjects:@"Workout"];
        [self deleteAllObjects:@"Route"];
        [self deleteAllObjects:@"WayPoint"];
        [self deleteAllObjects:@"Graphs"];
        [self deleteAllObjects:@"userSettings"];

        [self presentModalViewController:loginViewController animated:NO];
    }   
}


-(void)deleteAllObjects:(NSString *)entityDescription{
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:entityDescription inManagedObjectContext:managedObjectContext];
    [fetchRequest setEntity:entity];

    NSError *error;
    NSArray *items = [managedObjectContext executeFetchRequest:fetchRequest error:&error];
    [fetchRequest release];

    for (NSManagedObject *managedObject in items) {
        [managedObjectContext deleteObject:managedObject];
        NSLog(@"%@ object deleted", entityDescription);
    }

    if (![managedObjectContext save:&error]) {
        NSLog(@"Error deleting %@ - error:%@",entityDescription,error);
    }
}

It seems to be happening when the I log in and log out straight away. In this case there would be no objects on 4 of the 5 tables (note: userSettings would have 1 record).

Looking at the console the error message is '* Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'This NSPersistentStoreCoordinator has no persistent stores. It cannot perform a save operation.'', looking at the debugger its happening on the line if (![managedObjectContext save:&error]).

I'm not quite sure why this is happening, there would be no records in the table 'Workout' at this point, therefore there would be nothing to delete. The simulator seems to handle this with no problems.

Details of the Persistent Store code:

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {

    if (persistentStoreCoordinator != nil) {
        return persistentStoreCoordinator;
    }

    NSURL *storeUrl = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"LegginitCoreData.sqlite"]];

    NSError *error;
    persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: [self managedObjectModel]];
    if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:nil error:&error]) {
        // Handle error
    }    

    return persistentStoreCoordinator;
}

Any help would be much appreciated, I'm stuck on this one not and not sure where to go from here.

Regards, Stephen

+1  A: 

As Martin stated your error seems to be centered around the NSPersistentStore issue. Handing that error instead of having a comment of // Handle error would be a very good first step. It is quite possible you are getting an error right there and ignoring it.

In addition, on exit, if it is your goal to delete all data, there is an easier way.

Tear down the Core Data stack (releasing the NSManagedObjectContext, NSPersistentStoreCoordinator and the NSManagedObjectModel) and then delete the SQLite file. On next launch, all data is gone.

Marcus S. Zarra