-1

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

}
iOS dev
  • 2,254
  • 6
  • 33
  • 56
  • If you want to dispatch the block asynchronously the you will need to provide a completion handler block to invoke once the operation is complete. – Paulw11 Jun 23 '17 at 10:14
  • can you pelase elabrate @Paulw11 – iOS dev Jun 23 '17 at 11:50
  • 1
    How did you determine that executing this method asynchronously will address your muatated while enumerating crash? In general introducing concurrency is unlikely to help with such a crash, indeed it may make it harder to address. Why do you think this method is responsible for your crash? For people to be able to help you edit your question to include these answers and anything else you think might be relevant. HTH – CRD Jun 23 '17 at 17:02

2 Answers2

1
dispatch_async(dispatch_get_main_queue(), ^{
    //write your method body here
});
V-Dev
  • 488
  • 1
  • 4
  • 16
  • thank you @V-Dev but im getting this error as i 've NSArray return type ncompatible block pointer types sending 'NSArray *(^)(NSURLResponse *__strong, NSData *__strong, NSError *__strong)' to parameter of type 'void (^ _Nonnull)(NSURLResponse * _Nullable __strong, NSData * _Nullable __strong, NSError * _Nullable __strong)' – iOS dev Jun 23 '17 at 11:54
  • try to return array using call some method or notification centre don't return here if problem is related to returning data. you can get your array by notification center try that one trick. – V-Dev Jun 23 '17 at 12:06
1

try

-(void)getManagedObjectsArrayForEntity:(NSString*)entityName 
                               sortByFields:(NSArray*)sortByFields 
                                  predicate:(NSPredicate*)predicate
                                  ascending:(BOOL)ascending {

//your code here

//at last [nsnotification center code here]

}
V-Dev
  • 488
  • 1
  • 4
  • 16