Here's a way to write a method that we use block for data asynchronous download//block form of asynchronous request-(void) asyncdownloadwithblock{//Chrysanthemum [uiapplication sharedapplication] . networkactivityindicatorvisible = YES; Converts a string into a URL address nsurl *url = [Nsurl URLWithString:self.myURLStr]; Create an object Nsurlrequest *request = [Nsurlrequest Requestwithurl:url]; Get home row Nsoperationqueue *mainqueue = [Nsoperationqueue mainqueue]; NSLog (@ "current:%@", [Nsthread CurrentThread]); NSLog (@ "ismulti:%d", [Nsthread ismultithreaded]); NSLog (@ "Start download"); [Nsurlconnection sendasynchronousrequest:request queue:mainqueue completionhandler:^ (NSURLResponse *response, NSData *data, Nserror *connectionerror) {NSLog (@ "block callback, Download complete"); NSLog (@ "current:%@", [Nsthread CurrentThread]); NSLog (@ "%d", [Nsthread ismultithreaded]); Cancel Chrysanthemum [uiapplication sharedapplication].networkactivityindicatorvisible = NO; NSLog (@ "%@", response); Nshttpurlresponse *httprespone = (nshttpurlresponsE *) response; Status code if (Httprespone.statuscode! =) {NSLog (@ "Download error"); } if (data &&!connectionerror) {//note here to determine that the data exists and the connection is not error self.imageView.image = [UIImage imagewith Data:data]; } else{NSLog (@ "Download error"); } }]; NSLog (@ "current:%@", [Nsthread CurrentThread]); NSLog (@ "ismulti:%d", [Nsthread ismultithreaded]); NSLog (@ "************"); }
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Data download-block form of asynchronous request