ExecuteFetchRequest crashes on third call

I have a strange problem in my Objective-C code. I have a View Controller where I call my own loadData method in "viewWillAppear". This works cool until the view becomes visible a third time.

What caused the application without any exceptions or other prompts when called

NSArray *storeListArray = [managedObjectContext executeFetchRequest:fetchRequest error:&error]; 

I checked that managedObjectContext was not accidentally released somewhere else. But this is not so. FetchRequest is created immediately before the call above. I assume that it crashes somewhere inside the executeFetchRequest method.

Does anyone have an idea where I can find the error? What really annoys me is the fact that it is reproduced on the third call.

+4
source share
1 answer

Ok ... I solved the problem (I think). As far as I can see, the problem was the β€œerror” of the call.

I made a silly mistake by not initializing the NSError object before the call. So here is what works for me now:

 NSError *error = nil; NSArray *storeListArray = [MOC executeFetchRequest:fetchRequest error:&error]; 

Before that only

 NSError *error; 

What I found in many examples, by the way. But it looks like this was at least part of the problem.

All my code is as follows:

 MyAppDelegate *app = (MyAppDelegate *)[[UIApplication sharedApplication] delegate]; NSManagedObjectContext *MOC = [app managedObjectContext]; NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; NSEntityDescription *entity = [NSEntityDescription entityForName:@"Store" inManagedObjectContext:MOC]; [fetchRequest setEntity:entity]; NSSortDescriptor *streetDescriptor = [[NSSortDescriptor alloc] initWithKey:@"street" ascending:YES]; NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:streetDescriptor, nil]; [fetchRequest setSortDescriptors:sortDescriptors]; NSError *error = nil; NSArray *storeListArray = [MOC executeFetchRequest:fetchRequest error:&error]; 

Maybe this helps someone.

+1
source

All Articles