2013-02-21 85 views
0

我剛開始使用iOS中的NSOperationQueue,並面臨一些我想澄清的基本問題。在NSOperationQueue中添加操作

這是我使用的代碼:

-(void) SendRequestWithURL:(NSString*) URL andParam:(id) attributes { 

    [[AFNetworkActivityIndicatorManager sharedManager] incrementActivityCount]; 
    _m_singleton = [Singleton sharedSingleton]; 
    _parser = [[Syncparser alloc]init]; 
    NSString *strServURL = [NSString stringWithFormat:@"%@%@",_m_singleton.globalstrURLLink,URL]; 
    if (_theService == Item0 || _theService == Item1 || _theService == Item2){ 
     NSLog(@"Entrance _theService %i", _theService); 

    AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:strServURL]]; 
    [client postPath:@"POST" parameters:attributes 
      success:^(AFHTTPRequestOperation *operation, id responseObject) { 
       NSLog(@"_theService %i", _theService); 
       switch (_theService) { 
        case Item0: { 
         [_m_singleton.globalQueue addOperationWithBlock:^{ 
          NSLog(@"ParseItem0 %@", [operation responseString]); 
          [_parser ParseItem0:[operation responseString]]; 
         }]; 
        } 
         break; 
        case Item1:{ 
         [_m_singleton.globalQueue addOperationWithBlock:^{ 
          NSLog(@"ParseItem1 %@", [operation responseString]); 
          [_parser ParseItem1:[operation responseString]]; 
         }]; 
        } 
         break; 
        case Item2:{ 
         [_m_singleton.globalQueue addOperationWithBlock:^{ 
          NSLog(@"ParseItem2 %@", [operation responseString]); 
          [_parser ParseItem2:[operation responseString]]; 
         }]; 
        } 
         break; 
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
    } 
} 

我只叫物品1和item2.These是我在調試器中得到的值。

Entrance _theService 1 
Entrance _theService 2 
_theService 2 
_theService 1 

這顯然意味着item2首先被添加到隊列中,然後被item1添加到隊列中。

但令人驚訝的是,ParseItem1每次在ParseItem2之前被調用。即使_theService 1在_theService之前2.不知道爲什麼。對於noob問題抱歉。

需要一些指導。

回答

0

您是否嘗試過更改要添加的NSOperation的優先級? 以下是對的NSOperation優先級屬性的可能值: - NSOperationQueuePriorityVeryLow - NSOperationQueuePriorityLow - NSOperationQueuePriorityNormal - NSOperationQueuePriorityHigh - NSOperationQueuePriorityVeryHigh

您可能還希望,如果你想它來處理一個設置maxConcurrentOperationCount屬性設置爲1一次操作。

另一方面,您可以將_theService1的NSOperation的依賴項屬性設置爲值_theService2,以便_theService2必須等待_theService1完成後才能執行。

+0

謝謝你的建議,但爲什麼它總是以_theService1開頭,如果沒有設置依賴關係?我的問題是爲什麼總是固定到_theService1。基於邏輯,首先打印的服務首先應該先執行。 – lakesh 2013-02-21 08:10:42

+0

有兩件值得關注的文檔中的maxConcurrentOperationCount:「指定的值隻影響接收者及其隊列中的操作,其他操作隊列對象也可以並行執行其最大數量的操作。 減少併發操作的數量不會影響當前正在執行的任何操作。如果您指定值NSOperationQueueDefaultMaxConcurrentOperationCount(建議),則最大操作數可以根據系統條件動態更改。「 – 2013-02-21 15:15:45

0

這很明顯意味着item2首先被添加到隊列中, 後跟item1。

這個假設是錯誤的。 AFNetworking使用的NSOperationQueue不保證操作將按您添加它們的順序開始或完成。在臉上,在AFHTTPClient的init方法,你可以看到:

self.operationQueue = [[NSOperationQueue alloc] init]; 
[self.operationQueue setMaxConcurrentOperationCount:NSOperationQueueDefaultMaxConcurrentOperationCount]; 

所以你的隊列將被同時執行操作是肯定的。

我不能說爲什麼這個反向調用總是發生在你的測試中,但我認爲第二次調用要麼更快,要麼更快,或者更快解析,或者兩者兼而有之。

無論如何,如果您需要這兩個操作才能成功返回解析,則可以使用enqueueBatchOfHTTPRequestOperations:progressBlock:completionBlock:,並且完成塊在所有操作全部完成之前不會被調用。或者你可以設置一個操作依賴於另一個(因爲它是一個NSOperation子類)。

相關問題