【问题标题】:HTTPMaximumConnectionsPerHost is ignored in AFHTTPSessionManagerHTTPMaximumConnectionsPerHost 在 AFHTTPSessionManager 中被忽略
【发布时间】:2014-04-02 15:34:38
【问题描述】:

我正在尝试使用带有新 NSURLSession 类的 AFNetworking2.0 来获取一些数据。特别是我对 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 排队(或者至少让它们在某些限制后失败)?

谁能证实或反驳这种行为?

附:我已经实现了自己的排队代码,所以这个问题不是关于如何排队请求,而是如何使它与 NSURLSession 一起工作。

【问题讨论】:

    标签: ios objective-c afnetworking-2 nsurlsession


    【解决方案1】:

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

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

    【讨论】:

    • 我将描述我是如何理解这个过程的,如果我错了,请纠正我:我在获取第一部分数据时将任务记录为已启动。这意味着当我获取日志时已经建立了连接。由于它是 TCP 并且我的服务器不支持流水线,这意味着我只有在收到对当前请求的响应时才能发出另一个请求。从日志中我看到大约有 7 个任务开始了,甚至没有一个完成。所以我认为它不能正常工作。
    • 你找到解决办法了吗
    【解决方案2】:

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

    这看起来像一个错误,但它对我有用。 如果什么都不设置,我觉得默认是60秒,所以下一个任务会在60秒后开始,不管httpMaximumConnectionsPerHost是1。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多