2011-12-13 39 views

回答

2

ASINetworkQueue按照它們插入隊列的順序運行所有請求。它是先進先出(FIFO)系統。

如果你想確保它們都是一個接一個地運行而不是並行的,那麼你可以將併發設置爲1.隊列將從第一個請求開始並逐個運行,直到它到達最後一個請求

ASINetworkQueue *networkQueue = [[ASINetworkQueue alloc] init]; 

// Here we add all our 10 requests, the order in which we add 
// them determines the order they will execute 

// Set the concurrency to 1 and fire off the queue 
[networkQueue setMaxConcurrentOperationCount:1]; 
[networkQueue go]; 
+0

請注意,ASIHTTPRequest沒有積極開發了:http://allseeing-i.com/%5Brequest_release%5D – vikingosegundo 2011-12-14 00:52:09

2

檢查AFNetworking因爲不再維護ASIHTTPRequest。您可以使用它NSOperationQueue,其中有屬性maxConcurrentOperationCount。如果將其設置爲1:

將最大操作數設置爲1會有效地爲處理操作創建一個串行隊列。

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"https://gowalla.com/users/mattt.json"]]; 
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) { 
    NSLog(@"Name: %@ %@", [JSON valueForKeyPath:@"first_name"], [JSON valueForKeyPath:@"last_name"]); 
} failure:nil]; 

NSOperationQueue *queue = [[[NSOperationQueue alloc] init] autorelease]; 
[queue setMaxConcurrentOperationCount:1]; 
[queue addOperation:operation]; 
[queue addOperation:anotherOperation];