AFHTTPSessionManager中忽略HTTPMaximumConnectionsPerHost

时间:2014-04-02 15:34:39

标签: ios objective-c afnetworking-2 nsurlsession

我正在尝试将AFNetworking2.0与新的NSURLSession类一起使用来获取一些数据。特别是我对NSURLSessionConfiguration的新HTTPMaximumConnectionsPerHost属性感兴趣,但不幸的是它似乎没有像我期望的那样工作。

@interface ViewController ()
@property (nonatomic, strong) AFHTTPSessionManager *httpSessionManager;
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSURLSessionConfiguration *sessionConfig = [[NSURLSessionConfiguration defaultSessionConfiguration] copy];
    sessionConfig.HTTPMaximumConnectionsPerHost = 5;

    self.httpSessionManager = [[AFHTTPSessionManager alloc] initWithSessionConfiguration:sessionConfig];
    self.httpSessionManager.responseSerializer = [AFImageResponseSerializer serializer];

    for (int i = 0; i < 100; ++i) {
        //http://i.dailymail.co.uk/i/pix/2012/07/03/article-2168112-13E70C13000005DC-867_964x946.jpg // 400k picture, the one from deviantart is 2k
        [self.httpSessionManager GET:@"http://a.deviantart.net/avatars/t/o/toaster-kitten.jpg" parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
            NSLog(@"has finished task %p, context id %lu", task, task.taskIdentifier);
        } failure:^(NSURLSessionDataTask *task, NSError *error) {
            NSLog(@"has failed task %p, context id %lu", task, task.taskIdentifier);
        }];
    }
}

这是我的下载示例实现。

我还调整了AFHTTPSessionManager以生成日志:

- (NSURLSessionDataTask *)GET:(NSString *)URLString
                   parameters:(NSDictionary *)parameters
                      success:(void (^)(NSURLSessionDataTask *task, id responseObject))success
                      failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure
{
    NSMutableURLRequest *request = [self.requestSerializer requestWithMethod:@"GET" URLString:[[NSURL URLWithString:URLString relativeToURL:self.baseURL] absoluteString] parameters:parameters error:nil];

    __block NSURLSessionDataTask *task = [self dataTaskWithRequest:request completionHandler:^(NSURLResponse * __unused response, id responseObject, NSError *error) {
        if (error) {
            if (failure) {
                failure(task, error);
            }
        } else {
            if (success) {
                success(task, responseObject);
            }
        }
    }];

    [task addObserver:self forKeyPath:@"countOfBytesReceived" options:NSKeyValueObservingOptionOld context:(__bridge void*)task];
    NSLog(@"created task %p id %u", task, task.taskIdentifier);

    [task resume];

    return task;
}


- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
    if([[change objectForKey:@"old"] integerValue] == 0 && [keyPath isEqualToString:@"countOfBytesReceived"]){
        NSLog(@"dtask %p id %u: started", object, ((NSURLSessionDataTask*)object).taskIdentifier);
        @try {
            if ((__bridge void *)object == context) {
                [object removeObserver:self forKeyPath:@"countOfBytesReceived" context:(__bridge void*)object];
            }
        }
        @catch (NSException *exception) {
        }
    }
}

使用此代码,我收到下一个日志:

2014-04-02 17:10:38.950 test_block[2704:60b] created task 0x1701a4fa0 id 0
2014-04-02 17:10:38.972 test_block[2704:60b] created task 0x1781a5be0 id 1
2014-04-02 17:10:38.974 test_block[2704:60b] created task 0x1701a5160 id 2
2014-04-02 17:10:38.976 test_block[2704:60b] created task 0x1701a5320 id 3
2014-04-02 17:10:38.977 test_block[2704:60b] created task 0x1701a54e0 id 4
2014-04-02 17:10:38.979 test_block[2704:60b] created task 0x1701a56a0 id 5
2014-04-02 17:10:38.980 test_block[2704:60b] created task 0x1701a5940 id 6
2014-04-02 17:10:38.982 test_block[2704:1803] dtask 0x1701a4fa0 id 0: started
2014-04-02 17:10:38.984 test_block[2704:6403] dtask 0x1781a5be0 id 1: started
2014-04-02 17:10:38.986 test_block[2704:4b03] dtask 0x1701a56a0 id 5: started
2014-04-02 17:10:38.987 test_block[2704:5b03] dtask 0x1701a5160 id 2: started
2014-04-02 17:10:38.988 test_block[2704:6703] dtask 0x1701a5320 id 3: started
2014-04-02 17:10:38.990 test_block[2704:60b] created task 0x1781a5da0 id 7
2014-04-02 17:10:38.990 test_block[2704:1803] dtask 0x1701a54e0 id 4: started
2014-04-02 17:10:38.992 test_block[2704:6903] dtask 0x1701a5940 id 6: started
2014-04-02 17:10:38.993 test_block[2704:60b] created task 0x1701a4ec0 id 8
2014-04-02 17:10:38.993 test_block[2704:1803] dtask 0x1781a5da0 id 7: started
2014-04-02 17:10:38.994 test_block[2704:60b] created task 0x1781a5cc0 id 9
2014-04-02 17:10:38.996 test_block[2704:6403] dtask 0x1701a4ec0 id 8: started
2014-04-02 17:10:38.997 test_block[2704:60b] created task 0x1701a5240 id 10
2014-04-02 17:10:38.998 test_block[2704:5103] dtask 0x1781a5cc0 id 9: started
2014-04-02 17:10:38.999 test_block[2704:60b] created task 0x1781a6200 id 11
2014-04-02 17:10:39.391 test_block[2704:4603] dtask 0x1701a5240 id 10: started
2014-04-02 17:10:39.399 test_block[2704:60b] created task 0x1781a62e0 id 12
2014-04-02 17:10:39.406 test_block[2704:3507] dtask 0x1781a6200 id 11: started
2014-04-02 17:10:39.409 test_block[2704:60b] created task 0x1701a63c0 id 13
2014-04-02 17:10:39.411 test_block[2704:60b] created task 0x1701a6580 id 14
2014-04-02 17:10:39.414 test_block[2704:1803] dtask 0x1781a62e0 id 12: started
2014-04-02 17:10:39.418 test_block[2704:3507] dtask 0x1701a63c0 id 13: started
2014-04-02 17:10:39.421 test_block[2704:60b] created task 0x1781a6ac0 id 15
2014-04-02 17:10:39.423 test_block[2704:5b03] dtask 0x1701a6580 id 14: started
2014-04-02 17:10:39.425 test_block[2704:60b] created task 0x1701a5cc0 id 16
2014-04-02 17:10:39.427 test_block[2704:5b03] dtask 0x1781a6ac0 id 15: started
2014-04-02 17:10:39.428 test_block[2704:60b] created task 0x1701a5f60 id 17
2014-04-02 17:10:39.430 test_block[2704:5103] dtask 0x1701a5cc0 id 16: started
2014-04-02 17:10:39.431 test_block[2704:60b] created task 0x1781a64a0 id 18
2014-04-02 17:10:39.433 test_block[2704:3507] dtask 0x1701a5f60 id 17: started
2014-04-02 17:10:39.434 test_block[2704:60b] created task 0x1701a64a0 id 19
2014-04-02 17:10:39.435 test_block[2704:3507] dtask 0x1781a64a0 id 18: started
2014-04-02 17:10:39.437 test_block[2704:60b] created task 0x1701a6820 id 20
2014-04-02 17:10:39.438 test_block[2704:3507] dtask 0x1701a64a0 id 19: started
2014-04-02 17:10:39.440 test_block[2704:60b] created task 0x1781a6580 id 21
2014-04-02 17:10:39.441 test_block[2704:3507] dtask 0x1701a6820 id 20: started
2014-04-02 17:10:39.442 test_block[2704:60b] created task 0x1701a6120 id 22
2014-04-02 17:10:39.444 test_block[2704:3507] dtask 0x1781a6580 id 21: started
2014-04-02 17:10:39.445 test_block[2704:60b] created task 0x1701a7540 id 23
2014-04-02 17:10:39.446 test_block[2704:3507] dtask 0x1701a6120 id 22: started
2014-04-02 17:10:39.447 test_block[2704:60b] created task 0x1781a6c80 id 24
2014-04-02 17:10:39.448 test_block[2704:60b] created task 0x1701a5e80 id 25
2014-04-02 17:10:39.450 test_block[2704:60b] created task 0x1701a70e0 id 26
2014-04-02 17:10:39.450 test_block[2704:5103] dtask 0x1701a7540 id 23: started
2014-04-02 17:10:39.451 test_block[2704:5b03] dtask 0x1781a6c80 id 24: started
2014-04-02 17:10:39.453 test_block[2704:5103] dtask 0x1701a5e80 id 25: started
2014-04-02 17:10:39.454 test_block[2704:60b] created task 0x1701a7a80 id 27
...
2014-04-02 17:10:39.694 test_block[2704:60b] has finished task 0x1781aaaa0, context id 79
2014-04-02 17:10:39.695 test_block[2704:60b] has finished task 0x1701ac320, context id 80
2014-04-02 17:10:39.695 test_block[2704:60b] has finished task 0x1701abfa0, context id 78
2014-04-02 17:10:39.696 test_block[2704:60b] has finished task 0x1701ac4e0, context id 81
2014-04-02 17:10:39.697 test_block[2704:60b] has finished task 0x1701ac6a0, context id 82
2014-04-02 17:10:39.698 test_block[2704:60b] has finished task 0x1701ac860, context id 83
2014-04-02 17:10:39.699 test_block[2704:60b] has finished task 0x1781aac60, context id 84
2014-04-02 17:10:39.699 test_block[2704:60b] has finished task 0x1701aca20, context id 85
2014-04-02 17:10:39.700 test_block[2704:60b] has finished task 0x1781ab1a0, context id 86
2014-04-02 17:10:39.700 test_block[2704:60b] has finished task 0x1781ab360, context id 87
2014-04-02 17:10:39.701 test_block[2704:60b] has finished task 0x1701acbe0, context id 88
2014-04-02 17:10:39.702 test_block[2704:60b] has finished task 0x1701acda0, context id 89
2014-04-02 17:10:39.702 test_block[2704:60b] has finished task 0x1781ab6e0, context id 92
2014-04-02 17:10:39.703 test_block[2704:60b] has finished task 0x1781ab520, context id 90
2014-04-02 17:10:39.705 test_block[2704:60b] has finished task 0x1701ace80, context id 91
2014-04-02 17:10:39.713 test_block[2704:60b] has finished task 0x1781ab8a0, context id 94
2014-04-02 17:10:39.714 test_block[2704:60b] has finished task 0x1781aba60, context id 95
2014-04-02 17:10:39.714 test_block[2704:60b] has finished task 0x1701ad040, context id 93
2014-04-02 17:10:39.715 test_block[2704:60b] has finished task 0x1701ad200, context id 96
2014-04-02 17:10:39.715 test_block[2704:60b] has finished task 0x1701ad3c0, context id 97
2014-04-02 17:10:39.716 test_block[2704:60b] has finished task 0x1701ab520, context id 98
2014-04-02 17:10:39.716 test_block[2704:60b] has finished task 0x1701accc0, context id 99

这不是我期望看到的。正如您所看到的,至少在之前开始的任务完成之前,已经启动了5个以上的数据任务。

我使用iOS7.1和iOS7.0.3在iPhone上进行测试

有没有人可以解释为什么会发生这种情况以及如何让这些任务排队等待NSURLSession(或者至少让它们在一些限制后失败)?

有人可以确认或反驳这种行为吗?

P.S。我已经实现了自己的排队代码,所以这个问题不是关于如何对请求进行排队,而是如何使它与NSURLSession一起使用。

2 个答案:

答案 0 :(得分:1)

文档说明它将限制每个主机的连接数。这与创建的任务数量无关。

您可以轻松记录通知增量更新的委托回调,以查看哪些任务代表实际连接。其他任务在队列中,等待轮到他们连接到主机。

答案 1 :(得分:0)

我发现,对于后台会话,将timeoutIntervalForRequest设置为一个非常高的值实际上会使队列中的下一个任务等到上一个任务结束(或直到值timeoutIntervalForRequest为止结束了。

这看起来像一个错误,但它对我有用。 如果您没有设置任何内容,我认为默认值为60秒,因此下一个任务将在60秒后开始,无论httpMaximumConnectionsPerHost为1。