我剛開始使用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問題抱歉。
需要一些指導。
謝謝你的建議,但爲什麼它總是以_theService1開頭,如果沒有設置依賴關係?我的問題是爲什麼總是固定到_theService1。基於邏輯,首先打印的服務首先應該先執行。 – lakesh 2013-02-21 08:10:42
有兩件值得關注的文檔中的maxConcurrentOperationCount:「指定的值隻影響接收者及其隊列中的操作,其他操作隊列對象也可以並行執行其最大數量的操作。 減少併發操作的數量不會影響當前正在執行的任何操作。如果您指定值NSOperationQueueDefaultMaxConcurrentOperationCount(建議),則最大操作數可以根據系統條件動態更改。「 – 2013-02-21 15:15:45