首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSURLSession代理队列

NSURLSession代理队列
EN

Stack Overflow用户
提问于 2013-09-29 04:43:22
回答 2查看 6.7K关注 0票数 10

这听起来很奇怪。我似乎无法成功地将NSURLSession的delegateQueue设置为creation。

代码语言:javascript
复制
- (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);
}

这将导致以下输出。

代码语言:javascript
复制
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被忽视了。我在设备上和模拟器上都试过了。我还没有找到任何能解释这一点的文档。有人有洞察力吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-09-29 05:52:48

看起来,不管delegateQueue的getter怎么说,NSURLSession确实在使用您的NSOperationQueue。我为队列中的“操作”属性添加了KVO:

代码语言:javascript
复制
[queue addObserver:self forKeyPath:@"operations" options:NSKeyValueObservingOptionNew context:NULL];

并添加了以下方法:

代码语言:javascript
复制
- (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);

}

印出来:

代码语言:javascript
复制
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

因此,您可以看到委托确实是由队列处理的,尽管getter不这样说。真奇怪。

顺便说一句,您正在做的事情也正是AFNetworking做的,这通常是一个好的迹象:https://github.com/AFNetworking/AFNetworking/blob/master/AFNetworking/AFURLSessionManager.m#L287

票数 5
EN

Stack Overflow用户

发布于 2013-10-04 11:49:38

我确认了我这方面的问题,我试图设置一个由1元素组成的队列,就像在会话上使用maxConcurrentOperationCount=1一样,我看到几个任务同时运行,如下所示(URLSession:didWriteData:极大值totalBytesWritten):

代码语言:javascript
复制
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 

我还试图增加这个数字,并对我的queue=1进行计数。似乎是在自己管理队列

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19074483

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档