0
A
回答
0
這是一個常見的問題的一種特殊形式,是「我怎麼叫塊的一系列操作,並當最後一個完成得到通知?」
一個想法是使用每個請求的參數來做一個「待辦事項列表」。說每個請求需要一個數字0..99。現在,僞代碼將是這樣的:
@property(nonatomic, copy) void (^done)(BOOL); // we'll need to save a completion block
@property(nonatomic, strong) NSMutableArray *todo; // might as well save this too
- (void)makeRequestsThenInvoke:(void (^)(BOOL))done {
self.todo = [NSMutableArray arrayWithArray:@[@99, @98, @97 ... @0]];
// make this in a loop using real params to your network request (whatever distinguishes each request)
self.done = done;
[self makeRequests];
}
- (void)makeRequests {
if (!self.todo.count) { // nothing todo? then we're done
self.done(YES);
self.done = nil; // avoid caller-side retain cycle
return;
}
// otherwise, get the next item todo
NSNumber *param = [self.todo lastObject];
// build a url with param, e.g. http://myservice.com/request?param=%@ <- param goes there
[afManager post:url success:success:^(AFHTTPRequestOperation *operation, id responseObject) {
// handle the result
// now update the todo list
[self.todo removeLastObject];
// call ourself to do more, but use performSelector so we don't wind up the stack
[self performSelector:@selector(makeRequests) withObject:nil afterDelay:0.0];
}];
}
3
一對夫婦的想法:
如果真的只是要連續運行的每個請求(即一個接一個),你可以這樣做:
NSOperationQueue *queue = [[NSOperationQueue alloc] init]; queue.maxConcurrentOperationCount = 1; NSOperation *completionOperation = [NSBlockOperation blockOperationWithBlock:^{ NSLog(@"All operations done"); }]; for (NSInteger i = 0; i < operationCount; i++) { AFHTTPRequestOperation *operation = ... // create your operation here [completionOperation addDependency:operation]; [queue addOperation:operation]; } [queue addOperation:completionOperation];
注意,使用操作隊列這樣的優點是,你可以很容易地取消在隊列中,你應該永遠需要的所有操作。
如果這些執行順序是至關重要的,你可能想建立操作之間明確的相關性,如:
NSOperationQueue *queue = [[NSOperationQueue alloc] init]; queue.maxConcurrentOperationCount = 1; NSOperation *completionOperation = [NSBlockOperation blockOperationWithBlock:^{ NSLog(@"All operations done"); }]; NSOperation *priorOperation = nil; for (NSInteger i = 0; i < operationCount; i++) { AFHTTPRequestOperation *operation = ... // create your operation here [completionOperation addDependency:operation]; if (priorOperation) [operation addDependency:priorOperation]; [queue addOperation:operation]; priorOperation = operation; } [queue addOperation:completionOperation];
對我來說,問題是你是否絕對只是想運行一個在時間。你爲此付出了重大的性能損失。通常你會使用第一個代碼示例(其中唯一的顯式依賴關係是完成操作),並將
maxConcurrentOperationCount
設置爲4
之類的值,從而享受併發性和隨之而來的顯着性能增益(同時,將併發度限制爲一些合理的數字不會用完所有的工作線程,有請求超時的風險等)。你還沒有說過這100個操作是什麼,但是如果它是一堆下載,你可能需要考慮一個「延遲加載」模式,根據需要異步加載數據,而不是一次全部加載。
如果下載的圖像,例如你可以使用AFNetworking
UIImageView
類實現這一目標。
相關問題
- 1. 一個接一個地發送兩個ajax請求
- 2. 從一個請求發送JSON響應到另一個請求
- 3. 如何一個接一個地發送請求
- 4. 發送AJAX ASYNC請求一個接一個,間隔爲15秒
- 5. 一次發送一個請求到每一個URL Python
- 6. 發送一個Ajax請求的Spring MVC
- 7. 發送一個posthost請求到localhost
- 8. Angular發送一個http請求兩次
- 9. 一個asp,net controll發送ajax請求
- 10. 發送一個HTTP請求,使用PHP
- 11. NodeJS發送一個HTTPS SOAP請求
- 12. 窗體發送一個http請求
- 13. 發送一個請求到SOAP WSDL
- 14. 發送一個Ajax請求的PhoneGap
- 15. 發送JSON請求到一個Java Bean
- 16. 發送一個HTTP刪除請求
- 17. 發送一個Cookie在RestAngular請求
- 18. 發送一個HTTP請求W/JavaScript的
- 19. 發送請求到一個頁面
- 20. 發送一個AJAX POST請求
- 21. Alamofire一個接一個的請求數
- 22. 發送一個POST請求到一個來自C#的URL請求#
- 23. 發送一個HTTP POST請求,但JBOSS收到一個空的GET請求
- 24. 發送一個與另一個正在等待的請求衝突的請求
- 25. 從Android發送一個HTTP請求到一個rails服務器
- 26. 發送從一個站點到另一個站點的請求
- 27. 發送下一個請求時,最後一個完成
- 28. 發送請求從一個碼頭集裝箱到另一個
- 29. 發送一個$ _POST請求到另一個頁面沒有一個表格
- 30. 發送一批請求
小問題 - 您的選擇器名爲'makeRequests',但您調用'makeRequests:'。 –
啊。謝謝。將編輯。 – danh