iOS network programming synchronization, asynchronous, request queue 2014-12-7

Source: Internet
Author: User

1. Synchronization is intended for thread blocking, and using this method in the main thread will not respond to any user events. Therefore, in application design, most are used in specialized sub-threads to increase the user experience, or replace with asynchronous requests.

    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. }
    10. }

Get an instance of ASIHTTPRequest using Requestwithurl shortcut method

Startsynchronous method Initiates synchronous access

Because it is a synchronous request, there is no event-based callback method, so get the error message from the request's Error property

Responsestring, returning nsstring information for the request *

Note: Here I find that the Nsurlrequset and connect system APIs work together to achieve results. And no need to migrate open source code

2. The benefit of an asynchronous request is that it does not block the current thread, but is slightly more complex than the synchronization request, at least two callback methods are added to get the asynchronous event

  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. -(void) requestfinished: (ASIHTTPRequest *) request
  9. {
  10. Use when fetching text data
  11. NSString *responsestring = [request responsestring];
  12. Use when fetching binary data
  13. NSData *responsedata = [request ResponseData];
  14. }
  15. -(void) requestfailed: (ASIHTTPRequest *) request
  16. {
  17. Nserror *error = [request ERROR];
  18. }

Unlike above, a "delegate" is specified, and a startasynchronous is used to initiate a network request.

Here, two delegate methods are implemented, and when the data request succeeds, the requestfinished is called, and the request fails (such as a network problem or an internal server error) that calls requestfailed.

PS: Asynchronous requests are generally more commonly used, and the inside package is very good, at least more convenient than Symbian and other platforms, and can also modify the source code. Most of this is packaged with the queue to achieve the purpose of the image and the asynchronous download package (implemented).

3. The request queue provides a more granular control over asynchronous requests. For example, you can set the number of connections to synchronize 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 request is completed and the dequeue is placed in the queue for execution. This also applies when we have multiple request requests executed sequentially (either in business or software) and 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. Nsurl *url = [Nsurl urlwithstring:@ "http://allseeing-i.com"];
  7. ASIHTTPRequest *request = [ASIHTTPRequest Requestwithurl:url];
  8. [Request Setdelegate:self];
  9. [Request Setdidfinishselector: @selector (requestdone:)];
  10. [Request Setdidfailselector: @selector (requestwentwrong:)];
  11. [[Self queue] addoperation:request]; Queue is an Nsoperationqueue
  12. }
  13. -(void) Requestdone: (ASIHTTPRequest *) request
  14. {
  15. NSString *response = [request responsestring];
  16. }
  17. -(void) Requestwentwrong: (ASIHTTPRequest *) request
  18. {
  19. Nserror *error = [request ERROR];
  20. }

Create Nsoperationqueue, the task queue for the Cocoa architecture's execution Task (nsoperation). We can see through the source code of ASIHTTPRequest.h that this class is itself a nsoperation subclass. In other words, it can be placed directly in the "task queue" and executed.

iOS network programming synchronization, asynchronous, request queue 2014-12-7

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.