这听起来很奇怪。我似乎无法成功地将NSURLSession的delegateQueue设置为creation。
- (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被忽视了。我在设备上和模拟器上都试过了。我还没有找到任何能解释这一点的文档。有人有洞察力吗?
发布于 2013-09-29 05:52:48
看起来,不管delegateQueue的getter怎么说,NSURLSession确实在使用您的NSOperationQueue。我为队列中的“操作”属性添加了KVO:
[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因此,您可以看到委托确实是由队列处理的,尽管getter不这样说。真奇怪。
顺便说一句,您正在做的事情也正是AFNetworking做的,这通常是一个好的迹象:https://github.com/AFNetworking/AFNetworking/blob/master/AFNetworking/AFURLSessionManager.m#L287
发布于 2013-10-04 11:49:38
我确认了我这方面的问题,我试图设置一个由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 我还试图增加这个数字,并对我的queue=1进行计数。似乎是在自己管理队列。
https://stackoverflow.com/questions/19074483
复制相似问题