I am showing data in table view using NSFetchedResultsController
. Now when data reaches from server I need to delete all data present in the sqlite database.
Now when I delete data from database using given below code it sometime crashes (not always) giving this error:
Execution_BAD-ACCESS (code=2, address=0x0)
on this line
if (![moc save:&saveError]) {
.h
@property (readonly, retain, nonatomic) NSManagedObjectContext *managedObjectContext;
@property (readonly, retain, nonatomic) NSManagedObjectModel *managedObjectModel;
@property (readonly, retain, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;
.m
@synthesize managedObjectContext = _managedObjectContext;
@synthesize managedObjectModel = _managedObjectModel;
@synthesize persistentStoreCoordinator = _persistentStoreCoordinator;
NSManagedObjectContext *moc = [delegate managedObjectContext];
NSFetchRequest * allCategories = [[NSFetchRequest alloc] init];
[allCategories setEntity:[NSEntityDescription entityForName:@"Categories" inManagedObjectContext:moc]];
[allCategories setIncludesPropertyValues:NO]; //only fetch the managedObjectID
NSError * error = nil;
NSArray * dataArray = [moc executeFetchRequest:allCategories error:&error];
//error handling goes here
[NSFetchedResultsController deleteCacheWithName:@"RootDetail"];
for (Categories *cat in dataArray) {
[moc deleteObject:cat];
}
NSError *saveError = nil;
if (![moc save:&saveError]) {
NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
}
[allCategories release];
I check throughly now i found that this problem is coming when i vist the DetailPageController and go back(using UINavigationController popNavigationController:) and then if i wist DetailPageController then it crashes.
giving following errror -[DetailPageController controllerWillChangeContent:]: message sent to deallocated instance 0x11f52a90*
The problem is of NSManageObjectContext. So the fix is always use new created object of NSManageObjectContext otherwise it will create problems.