Synchronous, asynchronous, and request queue for iOS Network Programming

Source: Internet
Author: User

1. Synchronization means thread blocking. Using this method in the main thread will not respond to any user event. Therefore, in application design, most of them are used in specialized sub-threads to increase user experience or use asynchronous requests instead.

 
 
  1. - (IBAction)grabURL:(id)sender  
  2. {  
  3. NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com"];  
  4. ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];  
  5. [request startSynchronous];  
  6. NSError *error = [request error];  
  7. if (!error) {  
  8. NSString *response = [request responseString];  
  9. }  

Use the requestWithURL shortcut to obtain an instance of ASIHTTPRequest

StartSynchronous method to start synchronous access

Because it is a synchronous request and there is no event-based callback method, the error information is obtained from the error attribute of the request.

ResponseString, which is the NSString information returned by the request *

Note: Here I find that NsUrlRequset and connect system APIs can work together to achieve the effect. You do not need to port the source code.

2. The advantage of asynchronous requests is that the current thread is not blocked, but it is a little complicated than synchronous requests. At least two callback methods must be added to obtain asynchronous events.

 
 
  1. - (IBAction)grabURLInBackground:(id)sender  
  2. {  
  3. NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com"];  
  4. ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];  
  5. [request setDelegate:self];  
  6. [request startAsynchronous];  
  7. }  
  8.  
  9. - (void)requestFinished:(ASIHTTPRequest *)request  
  10. {  
  11. // Use when fetching text data  
  12. NSString *responseString = [request responseString];  
  13.  
  14. // Use when fetching binary data  
  15. NSData *responseData = [request responseData];  
  16. }  
  17.  
  18. - (void)requestFailed:(ASIHTTPRequest *)request  
  19. {  
  20. NSError *error = [request error];  

Different from the above, a "delegate" is specified and startAsynchronous is used to start the network request.

Two delegate methods are implemented here. When a data request is successful, requestFinished is called, and requestFailed is called when the request fails (such as a network problem or an internal server error.

PS: asynchronous requests are generally more commonly used, and the encapsulation is quite good, at least more convenient than platforms such as symbian, and the source code can be modified. Most of these packages are mixed with queues for image and asynchronous download (implemented ).

3. The request queue provides more precise and rich control over asynchronous requests. For example, you can set the number of connections for Synchronous requests in the queue. When the number of request instances added to the queue is greater than maxConcurrentOperationCount, the request instance is set to wait until at least one of the preceding requests is completed and columns are placed in the queue for execution. This also applies when we have multiple request needs to be executed in sequence (may be business needs or software tuning ), you only need to set maxConcurrentOperationCount to "1 ".

 
 
  1. - (IBAction)grabURLInTheBackground:(id)sender  
  2. {  
  3. if (![self queue]) {  
  4. [self setQueue:[[[NSOperationQueue alloc] init] autorelease]];  
  5. }  
  6.  
  7. NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com"];  
  8. ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];  
  9. [request setDelegate:self];  
  10. [request setDidFinishSelector:@selector(requestDone:)];  
  11. [request setDidFailSelector:@selector(requestWentWrong:)];  
  12. [[self queue] addOperation:request]; //queue is an NSOperationQueue  
  13. }  
  14.  
  15. - (void)requestDone:(ASIHTTPRequest *)request  
  16. {  
  17. NSString *response = [request responseString];  
  18. }  
  19.  
  20. - (void)requestWentWrong:(ASIHTTPRequest *)request  
  21. {  
  22. NSError *error = [request error];  

Create NSOperationQueue, the task queue for executing tasks (NSOperation) in the Cocoa architecture. We can see from the source code of ASIHTTPRequest. h that this class itself is a subclass of NSOperation. That is to say, it can be directly put into the "task queue" and executed

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.