使用dispatch_group_t的AFNetworking请求队列

时间:2016-05-18 10:34:34

标签: ios objective-c afnetworking grand-central-dispatch

我想在队列中逐个发送消息。换句话说,我需要在收到上一个请求的响应后发送新请求。我将dispatch_group_tAFNetworking一起使用但是它没有按预期工作(我知道operationQueue还有其他方法)。这是我的控制器:

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

结果如下:

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!

如何解决这个问题?

2 个答案:

答案 0 :(得分:2)

第二次尝试。这个对我有用。你错放了dispatch_group_wait。把它放在for循环的开头,它应该工作。否则,等待所有请求已经在路上。

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

答案 1 :(得分:1)

您可以通过在请求完成后再次调用sendTest方法来递归执行此操作。这是如何做。我没有测试代码,所以你可能需要稍微修改它。如果您有任何问题,请随时询问。

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

    }];

}

输出:

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