Soheil Novinfard Soheil Novinfard - 6 months ago 69
iOS Question

queue of AFNetworking requests with dispatch_group_t

I want to send messages one by one in a queue. In other words I need to send new request after I get the response of the previous request. I use

dispatch_group_t
with
AFNetworking
But it does not work as I expected (I know there are other ways with
operationQueue
). Here is my controller:

- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];

dispatch_async(queue, ^{
[self sentTest];
});
}

- (void) sentTest{
dispatch_group_t group = dispatch_group_create();
NSLog(@"Start ...");
for(int i = 1; i <= 10; i++)
{
dispatch_group_enter(group);
NSLog(@"sending message %d ...", i);

NSMutableDictionary *params = [@{@"param1": @"value1",
@"param2": @"value2",
} mutableCopy];
NSString *webServiceUrl = @"MY_REST_SERVICE_URL";

AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] init];
manager.responseSerializer = [AFJSONResponseSerializer serializer];

[manager POST:webServiceUrl parameters:params success:^(NSURLSessionDataTask *task, id responseObject) {
NSLog(@"message sent successful %d", i);
dispatch_group_leave(group);
} failure:^(NSURLSessionDataTask *task, NSError *error) {
NSLog(@"message sent failure %d", i);
dispatch_group_leave(group);
}];

}
dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
NSLog(@"Finished!");
}


The result is as follow:

2016-05-18 14:46:32.350 my_app[24514:29642637] Start ...
2016-05-18 14:46:32.350 my_app[24514:29642637] sending message 1 ...
2016-05-18 14:46:32.352 my_app[24514:29642637] sending message 2 ...
2016-05-18 14:46:32.353 my_app[24514:29642637] sending message 3 ...
2016-05-18 14:46:32.353 my_app[24514:29642637] sending message 4 ...
2016-05-18 14:46:32.354 my_app[24514:29642637] sending message 5 ...
2016-05-18 14:46:32.355 my_app[24514:29642637] sending message 6 ...
2016-05-18 14:46:32.356 my_app[24514:29642637] sending message 7 ...
2016-05-18 14:46:32.357 my_app[24514:29642637] sending message 8 ...
2016-05-18 14:46:32.358 my_app[24514:29642637] sending message 9 ...
2016-05-18 14:46:32.359 my_app[24514:29642637] sending message 10 ...
2016-05-18 14:46:33.738 my_app[24514:29642545] message sent successful 1
2016-05-18 14:46:34.818 my_app[24514:29642545] message sent successful 2
2016-05-18 14:46:35.833 my_app[24514:29642545] message sent successful 8
2016-05-18 14:46:36.955 my_app[24514:29642545] message sent successful 5
2016-05-18 14:46:37.957 my_app[24514:29642545] message sent successful 9
2016-05-18 14:46:39.017 my_app[24514:29642545] message sent successful 4
2016-05-18 14:46:40.034 my_app[24514:29642545] message sent successful 3
2016-05-18 14:46:41.115 my_app[24514:29642545] message sent successful 10
2016-05-18 14:46:42.233 my_app[24514:29642545] message sent successful 6
2016-05-18 14:46:43.253 my_app[24514:29642545] message sent successful 7
2016-05-18 14:46:43.253 my_app[24514:29642637] Finished!


How to solve this problem?

Answer

Second try. This one works for me. You misplaced the dispatch_group_wait. Put it at the beginning of the for loop and it should work. Otherwise you wait when all the requests are already on their way.

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

    dispatch_async(queue, ^{
        [self sendTest];
    });


}

- (void) sendTest{

    dispatch_group_t group = dispatch_group_create();

    NSLog(@"Start ...");

    for(int i = 1; i <= 10; i++)
    {
        dispatch_group_wait(group, DISPATCH_TIME_FOREVER);

        dispatch_group_enter(group);

            NSLog(@"sending message %d ...", i);

            NSString *webServiceUrl = @"http://ws.audioscrobbler.com/2.0/?method=artist.search&artist=Artist&api_key=c0e97cf5fe59ad032059fb7b2eaab414&format=json";

            AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] init];
            manager.responseSerializer = [AFJSONResponseSerializer serializer];



            [manager GET:webServiceUrl parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {


        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {

            NSLog(@"message sent successful %d", i);

            dispatch_group_leave(group);

        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            NSLog(@"message sent failure %d", i);

            dispatch_group_leave(group);

        }];

    }

    NSLog(@"Finished!");
}
Comments