queue of AFNetworking requests with dispatch_group_t

谁都会走 提交于 2019-12-11 04:25:53

问题


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?


回答1:


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!");
}



回答2:


You could do this recursively by calling your sendTest method again and again after the request is finished. Here´s how. I didn´t test the code so it could be you have to modify it a bit. Feel free to ask if you have any questions.

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

    NSLog(@"Start ...");

    [self sentTest:0];
}

- (void) sentTest:(int)i{

    if(i >= 10)
    {
        NSLog(@"Finished");
        return;
    }

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

        __block int blockI = i + 1;

        [self sentTest:blockI];

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

    }];

}

Output:

2016-05-19 10:12:48.460 TestAFNetworking[75972:21147962] Start ...
2016-05-19 10:12:48.460 TestAFNetworking[75972:21147962] sending message 0 ...
2016-05-19 10:12:49.014 TestAFNetworking[75972:21147962] message sent successful 0
2016-05-19 10:12:49.015 TestAFNetworking[75972:21147962] sending message 1 ...
2016-05-19 10:12:49.524 TestAFNetworking[75972:21147962] message sent successful 1
2016-05-19 10:12:49.525 TestAFNetworking[75972:21147962] sending message 2 ...
2016-05-19 10:12:50.126 TestAFNetworking[75972:21147962] message sent successful 2
2016-05-19 10:12:50.127 TestAFNetworking[75972:21147962] sending message 3 ...
2016-05-19 10:12:50.637 TestAFNetworking[75972:21147962] message sent successful 3
2016-05-19 10:12:50.637 TestAFNetworking[75972:21147962] sending message 4 ...
2016-05-19 10:12:51.477 TestAFNetworking[75972:21147962] message sent successful 4
2016-05-19 10:12:51.477 TestAFNetworking[75972:21147962] sending message 5 ...
2016-05-19 10:12:52.257 TestAFNetworking[75972:21147962] message sent successful 5
2016-05-19 10:12:52.257 TestAFNetworking[75972:21147962] sending message 6 ...
2016-05-19 10:12:52.595 TestAFNetworking[75972:21147962] message sent successful 6
2016-05-19 10:12:52.595 TestAFNetworking[75972:21147962] sending message 7 ...
2016-05-19 10:12:52.937 TestAFNetworking[75972:21147962] message sent successful 7
2016-05-19 10:12:52.938 TestAFNetworking[75972:21147962] sending message 8 ...
2016-05-19 10:12:53.540 TestAFNetworking[75972:21147962] message sent successful 8
2016-05-19 10:12:53.540 TestAFNetworking[75972:21147962] sending message 9 ...
2016-05-19 10:12:54.043 TestAFNetworking[75972:21147962] message sent successful 9
2016-05-19 10:12:54.044 TestAFNetworking[75972:21147962] Finished


来源:https://stackoverflow.com/questions/37297033/queue-of-afnetworking-requests-with-dispatch-group-t

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!