2013-09-29 71 views
8

这似乎很奇怪。我似乎无法在创建时成功设置NSURLSession的delegateQueue。NSURLSession委托队列

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    NSOperationQueue *queue = [[NSOperationQueue alloc] init]; 
    queue.maxConcurrentOperationCount = 5; 

    NSLog(@"The queue before: %@", queue); 
    NSLog(@"Max op Count before: %i", queue.maxConcurrentOperationCount); 

    NSURLSession *session = [NSURLSession sessionWithConfiguration:nil 
                  delegate:self 
                delegateQueue:queue]; 

    NSLog(@"The queue after: %@", session.delegateQueue); 
    NSLog(@"Max op Count after : %i", session.delegateQueue.maxConcurrentOperationCount); 
} 

这将导致以下输出。

The queue before: <NSOperationQueue: 0x16d99160>{name = 'NSOperationQueue 0x16d99160'} 
Max op Count before: 5 
The queue after: <NSOperationQueue: 0x16d77a50>{name = 'NSOperationQueue 0x16d77a50'} 
Max op Count after : 1 

它看起来像我的delegateQueue被忽略。我已经在设备上以及模拟器上尝试过了。我还没有找到任何可以解释这个问题的文档。有没有人有任何见解?

回答

5

看起来,尽管delegateQueue的getter表示,NSURLSession的确在使用您的NSOperationQueue。我加入志愿的“操作”属性的队列:

[queue addObserver:self forKeyPath:@"operations" options:NSKeyValueObservingOptionNew context:NULL]; 

,并添加了以下方法:

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context 
{ 
    NSLog(@"%@%@", object, change); 
    NSOperationQueue *queue = (NSOperationQueue *)object; 

    NSLog(@"The queue in KVO: %@", queue); 
    NSLog(@"Max op Count in KVO: %i", queue.maxConcurrentOperationCount); 

    NSLog(@"%@", self.session.delegateQueue); 

} 

而且它打印:

2013-09-29 07:45:13.751 sotest1[17214:1403] <NSOperationQueue: 0x895e0c0>{name = 'NSOperationQueue 0x895e0c0'} 
2013-09-29 07:45:13.757 sotest1[17214:4207] <NSOperationQueue: 0x98a0940>{name = 'NSOperationQueue 0x98a0940'}{ 
    kind = 1; 
    new =  (
     "<NSBlockOperation: 0x9a52370>" 
    ); 
} 
2013-09-29 07:45:13.757 sotest1[17214:4207] The queue in KVO: <NSOperationQueue: 0x98a0940>{name = 'NSOperationQueue 0x98a0940'} 
2013-09-29 07:45:13.757 sotest1[17214:4207] Max op Count in KVO: 5 

所以,你可以看到代表确实会被你的队列处理,尽管吸气者另有说明。奇怪的。

顺便说一句,你这样做的方式也正是AFNetworking这么做,这通常是一个好兆头:https://github.com/AFNetworking/AFNetworking/blob/master/AFNetworking/AFURLSessionManager.m#L287

2

我确认我这边的问题,我试图设置1元的队列,你没有使用maxConcurrentOperationCount = 1上的会话我看到如通过在以下记录描述在同时运行多个任务(URLSession:didWriteData:totalBytesWritten):

NSLog(@"Download %.2f%% task=%d - operationInQueue=%d maxOperation=%d ", result, [downloadTask taskIdentifier], [self.bkgQueue operationCount],[self.bckQueue maxConcurrentOperationCount]); 

Download 1.80% task=2 - operationInQueue=2 maxOperation=1 
Download 1.15% task=1 - operationInQueue=1 maxOperation=1 
Download 1.49% task=1 - operationInQueue=1 maxOperation=1 
Download 1.51% task=1 - operationInQueue=1 maxOperation=1 
Download 1.14% task=0 - operationInQueue=1 maxOperation=1 
Download 3.90% task=2 - operationInQueue=2 maxOperation=1 
Download 1.14% task=0 - operationInQueue=1 maxOperation=1 
Download 1.85% task=1 - operationInQueue=1 maxOperation=1 
Download 1.87% task=1 - operationInQueue=1 maxOperation=1 

我也试图增加该号码和依靠我的队列= 1。 它似乎是他自己管理队列