2013-09-29 9 views
8

Questo sembra strano. Sembra che non riesca a impostare correttamente un delegateQueue di NSURLSession alla creazione.NSURLSessione coda delegati

- (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); 
} 

Ciò genera l'output seguente.

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 

Sembra che il mio delegateQueue venga ignorato. Ho provato sul dispositivo e sul simulatore. Non ho trovato alcuna documentazione che spiegherebbe questo. Qualcuno ha qualche idea?

risposta

5

Sembra, nonostante ciò che dice il getter per delegateQueue, NSURLSession sta effettivamente utilizzando NSOperationQueue. Ho aggiunto KVO per le "operazioni" di proprietà sulla coda:

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

e ha aggiunto il seguente metodo:

- (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); 

} 

E STAMPE:

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 

modo da poter vedere il delegato effettivamente viene elaborato dalla tua coda, nonostante il getter dica altrimenti. Strano.

Btw, il modo in cui si sta facendo è anche esattamente AFNetworking fa, che è generalmente un buon segno: https://github.com/AFNetworking/AFNetworking/blob/master/AFNetworking/AFURLSessionManager.m#L287

2

Confermo il problema da parte mia ho cercato di impostare una coda di 1 elemento, come si ha fatto utilizzando la maxConcurrentOperationCount = 1 sulla sessione vedo diverse attività in esecuzione nello stesso tempo come descritto dal login di seguito nella (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 

ho anche cercato di aumentare questo numero e un conta sulla mia coda = 1. Sembra che stia gestendo da solo la coda.