When iOS solves concurrent requests, it only accepts the results returned by the last request.

Source: Internet
Author: User

When iOS solves concurrent requests, it only accepts the results returned by the last request.

Although NSOperation cancel cannot cancel the request, it can mark this NSOperation. When the cancel attribute is YES, it indicates that although NSOperation has been executed and the result is returned, it is not required now. Therefore, the last operation cancel is dropped every time the request is executed, and then the operation Block is used to determine whether it is cancel. If cancel is used, the value of response is not returned.
1 + (AFHTTPRequestOperation *) parameters :( operator) successBlock 2 {3 operator * manager = [operator manager]; 4 5 AFHTTPRequestOperation * operation = [manager GET: kRequestSellerURL parameters: nil success: ^ (AFHTTPRequestOperation * operation, id responseObject) {6 7 if (operation. isCancelled) {8 // If operation has been Canceled, the returned data will not be processed 9 NSLog (@ "operation is Canceled "); 10} else {11 // normal data parsing 12 NSArray * sellerArray = [MTLJSONAdapter modelsOfClass: [Seller class] fromJSONArray: responseObject [@ "data"] error: nil]; 13 if (successBlock) {14 // NSLog (@ "current operation count is % d", [manager. operationQueue operations]. count); 15 successBlock (sellerArray); 16} 17} 18 19 20} failure: ^ (AFHTTPRequestOperation * operation, NSError * error) {21 22}];

 

Then do this in the for loop. for (int I = 1; I <= 10; I ++) {if (operation) {[operation cancel];} operation = [Seller requestSellerWithCompletion: ^ (id object) {NSLog (@ "finished download % d", I) ;}];}

 

 

Run the command to check the console.

I saw an article by Ye Gucheng, which is summarized as above.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.