Waiting for nested AFNetworking calls and for loops to finish

afnetworking, ios, objective-c

Solution

You are looking for `dispatch_group_notify` and `dispatch_group_enter`/`dispatch_group_leave`.

- `dispatch_group_notify` executes the given block in the given queue, when every block in the group is finished.

- `dispatch_group_enter` increases the current count of executing tasks in the group. Every `dispatch_group_enter` must be balanced with a call to `dispatch_group_leave`.

- `dispatch_group_leave` decreases the current count of executing tasks in the group.

So, you should trick `dispatch_group_notify` with increase the number of the tasks in the group before your network calls start and decrease it when everything finished. To achieve this, call `dispatch_group_enter` before `dispatch_async` and call `dispatch_group_leave` in the last thread. Since you know the element count of the every result array, you can check if the current thread is the last one.

dispatch_group_enter(group); // Increases the number of blocks in the group.
dispatch_async(queue, ^{
   // Make your AFNetworking calls.
    dispatch_group_async(group, queue, ^{
          //do some work. 
          if (isLastThread)
               dispatch_group_leave(group); // Decreases the number of blocks in the group.
    });
});

dispatch_group_notify(group, dispatch_get_main_queue(), ^{ // Calls the given block when all blocks are finished in the group.
     // All blocks finished, do whatever you like.
});

Problem

So, I've been searching all over but didn't find a solution, or at least I couldn't apply it. I've found this thread here on stackoverflow, but didn't succeed in implementing it in my code. My issue is, that I need to know when nested AFNetworking calls and For loops are done. I've tried it with GCD groups, but with no luck. The code looks like this: ``` { dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); dispatch_group_t group = dispatch_group_create(); dispatch_async(queue, ^{ [[JSON GET method using AFNetworking 2.0] success:^(NSArray *result) { dispatch_group_async(group, queue, ^{ //do some work with the result for (NSDictionary *resultPartDictionary in result) { dispatch_group_async(group, queue, ^{ //do some more work with parts of the result [[JSON GET method based on result] success:^(NSArray *result) { dispatch_group_async(group, queue, ^{ //do some work for (NSDictionary *resultPartDictionary in result) { dispatch_group_async(group, queue, ^{ //do some work [[JSON GET method based on result] success:^(NSArray *result) { dispatch_group_async(group, queue, ^{ //do some work for (NSDictionary *resultPartDictionary in result) { dispatch_group_async(group, queue, ^{ //do some work }); } }); }]; }); } }); }]; }); } }); } }); } ``` Right now, everything works. I'm handling Core Data inside the blocks, so I needed MOCs for every thread, which works as well. The only thing I'd like to know is how to know when all these blocks finish. Thank you! EDIT So, I've tried using `dispatch_group_enter(group)` and `dispatch_group_leave(group)`, but it seems to me, that it's just not possible with this embedded architecture. Because of the For loops, the "leave" notifications are either too many, which causes an exception or not enough and the `dispatch_group_notify` returns too early. Any ideas on this?

Original source

Related problems