Data download-block form of asynchronous request

Source: Internet
Author: User

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

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.