For some reasons to fix some crashes i need write / call the following method in GCD, But as it is having return type NSArray here dispatch blocks do not allow to write this method in dispatch blocks.
Actual crash is Terminating app due to uncaught exception 'NSGenericException', reason: '*** Collection <__NSCFSet: 0x6080002531a0> was mutated while being enumerated.'
Can someone please help me on this
-(NSArray*)getManagedObjectsArrayForEntity:(NSString*)entityName
sortByFields:(NSArray*)sortByFields
predicate:(NSPredicate*)predicate
ascending:(BOOL)ascending {
// Create the fetch request for the entity.
NSFetchRequest *fetchRequest = [[[NSFetchRequest alloc] init] autorelease];
// Edit the entity name as appropriate.
NSEntityDescription *entity = [NSEntityDescription entityForName:entityName inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
// Set the batch size to a suitable number.
[fetchRequest setFetchBatchSize:20];
[fetchRequest setFetchLimit:-1];
// Set the sort descriptor
if (sortByFields) {
NSMutableArray *sortDescriptors = [NSMutableArray array];
for (NSString *sortByField in sortByFields) {
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:sortByField ascending:ascending];
[sortDescriptors addObject:sortDescriptor];
[sortDescriptor release];
}
[fetchRequest setSortDescriptors:sortDescriptors];
}
if (predicate) {
[fetchRequest setPredicate:predicate];
//DDLogVerbose(@"Predicate: %@", [predicate description]);
}
NSLog(@"HAS CHANGES %d",self.managedObjectContext.hasChanges);
NSError *error;
//===
return [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];
}