StartAsynchronous of ASIHTTPRequest calls EXC_BAD_ACCESS

Source: Internet
Author: User

A simple iOS application implements the ASIHTTPRequest delegate method-(void) requestFinished :( ASIHTTPRequest *) request and-(void) requestFailed :( ASIHTTPRequest *) request in the master View Controller class. In the viewDidLoad method of this class, the ASIHTTPRequest asynchronous request is called. The request object is defined as retain, such as @ property (retain) ASIHTTPRequest * theRequest; In the-(void) viewDidLoad method, the method in the called class is this. -(IBAction) showCheckcode :( id) sender {NSString * imgURL = @ "https://dynamic.12306.cn/otsweb/passCodeAction.do? Rand = sjrand "; self. theRequest = [ASIHTTPRequest requestWithURL: [NSURL URLWithString: imgURL]; self. theRequest. accessibilityLabel = @ "imgCode"; [self. theRequest settag: 100]; [self. theRequest setValidatesSecureCertificate: NO]; // [theRequest applyCookieHeader]; self. theRequest. delegate = (id <ASIHTTPRequestDelegate>) self; [self. theRequest startAsynchronous];} and, in the-(void) viewDidLoad method, call another custom class method to request an asynchronous Operation. This class also implements the ASIHTTPRequest delegate method. When the application runs, an error occurs. The error message on the console is EXC_BAD_ACCESS. The first response should be calm. Although this problem is very troublesome, it is obviously related to ASIHTTPRequest, and it is an open-source framework that is not familiar to me. The development of ASIHTTPRequest does not mean that it is no longer maintained, but ASIHTTPRequest is also a very good network access framework and is sufficient for general applications. ASIHTTPRequest is based on MRC, and my application is under ARC. Therefore, when dragging ASIHTTPRequest into the application package, you need to make a simple adjustment. 1. select target, and then select Build Phases label to expand Compile Sources; 2. in all and ASI .. after the related files (Compile Flags), add the compilation option "-fno-objc-arc". My development tool is XCODE4.5. Here, I would like to add more information. Analyze the ASIHTTPRequest framework, open the configuration debugging item in the ASIHTTPRequestConfig. h file, and then run it. Although the application is still crash, this error stops at a method in ASIHTTPRequest. m. (Void) requestReceivedResponseHeaders :( NSMutableDictionary *) newResponseHeaders {if ([self error] | [self mainRequest]) {return ;}--> if (delegate & [delegate respondsToSelector: didReceiveResponseHeadersSelector]) {the problem lies in the delegate settings. 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, in the implementation method of delegate, the request tag is used 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 NSString * responseString = [request responseString] is returned; // For binary data, NSData * responseData = [request responseData]; NSLog (@ "% @", responseString) ;}]; [request setFailedBlock: ^ {// request response failed, returned error message NSError * error = [request error]; NSLog (@ "error: % @", [error userInfo]);}]; the second method is much more convenient than the first method. Block can be used in iOS5.0. I didn't expect block to be so useful. To solve the problem, follow your practice. 1. How to call the open-source framework developed by MRC in ARC; 2. How to compile the framework, the differences between the real machine and the simulator, and the differences between the release version and the debug version. This is not mentioned in the document, but I also tried it, although the results failed. 3. multithreading. ASIHTTPRequest's asynchronous request is a multi-threaded call. 4. Some NSOperationQueue basics, but I still don't understand them yet. 5. More thorough understanding of delegate.

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.