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.
- - (IBAction)grabURL:(id)sender
- {
- NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com"];
- ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
- [request startSynchronous];
- NSError *error = [request error];
- if (!error) {
- NSString *response = [request responseString];
- }
- }
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.
- - (IBAction)grabURLInBackground:(id)sender
- {
- NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com"];
- ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
- [request setDelegate:self];
- [request startAsynchronous];
- }
-
- - (void)requestFinished:(ASIHTTPRequest *)request
- {
- // Use when fetching text data
- NSString *responseString = [request responseString];
-
- // Use when fetching binary data
- NSData *responseData = [request responseData];
- }
-
- - (void)requestFailed:(ASIHTTPRequest *)request
- {
- 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 ".
- - (IBAction)grabURLInTheBackground:(id)sender
- {
- if (![self queue]) {
- [self setQueue:[[[NSOperationQueue alloc] init] autorelease]];
- }
-
- NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com"];
- ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
- [request setDelegate:self];
- [request setDidFinishSelector:@selector(requestDone:)];
- [request setDidFailSelector:@selector(requestWentWrong:)];
- [[self queue] addOperation:request]; //queue is an NSOperationQueue
- }
-
- - (void)requestDone:(ASIHTTPRequest *)request
- {
- NSString *response = [request responseString];
- }
-
- - (void)requestWentWrong:(ASIHTTPRequest *)request
- {
- 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