[IOS] several minor issues found during the use of the asihttprequest Library

Source: Internet
Author: User

During iOS development, the asihttprequest library is the most commonly used network library, which is powerful and easy to use.

However, there are several minor issues when using this library. I have been searching and researching on the Internet for a long time. It is now recorded here.

Question 1:

One problem is that when there are many asynchronous requests and a large number of concurrent connections, some requests may fail.

Cause:

The Code defaults to a maximum of four concurrent connections, and other connections need to wait. Then, if a connection request is complete, the connection will be reused, but the connection will be closed. At this time, the code will not re-request again, causing the request to fail, returns nil.

Simple solution:

The simplest solution is to close the multiplexing connection. Every time a new connection is created, no problem will occur. Or only one connection is allowed at a time.

Add the following code to asihttprequest: [self setshouldattemptpersistentconnection: No];

Question 2:

Another problem is that exceptions occur occasionally, and exceptions occur occasionally after the startasynchronous call of asihttprequest.

In viewcontroller, the delegate method is used, that is, the code is added to the-(void) viewdidload method: [Request setdelegate: Self]

Then implement its delegate method-(void) requestfinished :( asihttprequest *) request and-(void) requestfailed :( asihttprequest *) Request

During the test, if you close and open this interface when the execution of the request is not completed, the exception exc_bad_access may occur.


Solution:

On stackoverflow.com, some netizens proposed two solutions.

First, use the same delegate and initialize it as soon as the application runs. If other classes want to call asynchronous requests, send delegate.
The call method is as follows:
-(Void) sendurl: (nsstring *) resturl withcallback :( nsobject *) delegate;
Then, the request tag is used in the delegate implementation method to identify the asynchronous call from which class.

Second, use block to directly implement asynchronous requests and discard delegate.

Use setcompletionblock: ^ to replace Delegate's setdidfinishselector ".

You can set a request, which is much easier than setting request. Delegate to implement delegate.

[Request setcompletionblock: ^ {
// When the request response ends, responsestring is returned.
Nsstring * responsestring = [Request responsestring]; // For binary data, use nsdata to return nsdata * responsedata = [Request responsedata];
Nslog (@ "% @", responsestring );
}];
[Request setfailedblock: ^ {
// If the request response fails, an error message is returned.
Nserror * error = [Request Error];
Nslog (@ "error: % @", [error userinfo]);
}];

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.