문제

I am using AFNetworking2 with two methods to first delete a row and then get the updated array of objects:

    [self.tableView beginUpdates];
    [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    NSDictionary * dict = [rapportsArray objectAtIndex:indexPath.row];
    [self deleteRapportWithID:[dict objectForKey:@"id"]];
    [self getRapports:interval];
    [self.tableView endUpdates];;

As you can see from here, the methods in question are deleteRapport and getRapports, as mentioned - both contain success and failure blocks, on success getRapports will update the tableview.

My intention is to use dispatch_async to wait until both methods are executed, can you help me with some suggestions of how to implement that?

도움이 되었습니까?

해결책

Look into dispatch_groups, you can tie a dispatch_group to a dispatch_queue and be notified when all items in the group are finished using dispatch_group_wait, even if you dispatch async to the queues.

Have a look at:

http://cocoasamurai.blogspot.com/2011/02/practical-design-patterns-with-blocks.html

To summarize:

dispatch_group_t group = dispatch_group_create();
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

dispatch_group_async(group, queue, ^{
    ... do some stuff ...
});

dispatch_group_async(group, queue, ^{
    ... do some other stuff ...
});

// wait until those async functions are done
dispatch_group_wait(group, DISPATCH_TIME_FOREVER);

... continue doing stuff ...
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top