Asinetworkqueue Queue Download

Source: Internet
Author: User

We introduced a request queue using an example, we designed an application where the user clicks the Go button to download two images from the server simultaneously to display in the screen.


We look directly at the main view controller ViewController.h code as follows:

#import "ASIHTTPRequest.h"

#import "ASINetworkQueue.h"

#import "Nsnumber+message.h"

#import "Nsstring+urlencoding.h"



@interface Viewcontroller:uiviewcontroller



@property (Weak, nonatomic) Iboutlet Uiimageview *imageview1;

@property (Weak, nonatomic) Iboutlet Uiimageview *imageview2;

@property (strong) Asinetworkqueue *networkqueue;



-(Ibaction) OnClick: (ID) sender;

@end

We need to introduce the ASI framework of the two header files ASIHTTPRequest.h and ASINetworkQueue.h. Where ImageView1 and ImageView2 are the two picture view controls that correspond to the screen. The Networkqueue property of the Asinetworkqueue type is also defined. We look directly at the main view controller VIEWCONTROLLER.M Click the Go button to invoke the method, the code is as follows:

-(Ibaction) OnClick: (ID) Sender {

if (!_networkqueue) {

_networkqueue = [[Asinetworkqueue alloc] init]; ①

}

Stop the previous queue

[_networkqueue cancelalloperations]; Ii

Create an ASI queue

[_networkqueue setdelegate:self];

[_networkqueue setrequestdidfinishselector: @selector (requestfinished:)];③

[_networkqueue setrequestdidfailselector: @selector (requestfailed:)];④

[_networkqueue setqueuedidfinishselector: @selector (queuefinished:)];⑤



for (int i=1; i<3; i++) {

NSString *strurl = [[NSString alloc] Initwithformat:

@ "Http://iosbook3/download.php?email=%@&filename=test%i.jpg",

@ "< your iosbook1.com user mailbox >", I];

Nsurl *url = [Nsurl urlwithstring:[strurl urlencodedstring];

ASIHTTPRequest *request = [ASIHTTPRequest Requestwithurl:url];

Request.tag = i; ⑥

[_networkqueue Addoperation:request]; ⑦

}

[_networkqueue go]; ⑧

}

Let's look at their callback methods, the code:

-(void) requestfinished: (ASIHTTPRequest *) request

{

NSData *data = [request ResponseData];

Nserror *eror;

Nsdictionary *resdict = [Nsjsonserialization jsonobjectwithdata:data

Options:nsjsonreadingallowfragments error:&eror];

if (!resdict) {

UIImage *img = [UIImage imagewithdata:data];

if (Request.tag ==1) {①

_imageview1.image = img;

} else {

_imageview2.image = img;

}

} else {

NSNumber *resultcodeobj = [resdict objectforkey:@ "ResultCode"];

NSString *errorstr = [Resultcodeobj errormessage];

Uialertview *alertview = [[Uialertview alloc] initwithtitle:@ "error message"

Message:errorstr

Delegate:nil

cancelbuttontitle:@ "OK"

Otherbuttontitles:nil];

[Alertview show];

}

if ([_networkqueue requestscount] = = 0) {②

[Self setnetworkqueue:nil];

}

NSLog (@ "request succeeded");

}

-(void) requestfailed: (ASIHTTPRequest *) Request③

{

Nserror *error = [request ERROR];

NSLog (@ "%@", [Error localizeddescription]);

if ([_networkqueue requestscount] = = 0) {

[Self setnetworkqueue:nil];

}

NSLog (@ "request failed");

}

-(void) queuefinished: (ASIHTTPRequest *) Request④

{

if ([_networkqueue requestscount] = = 0) {

[Self setnetworkqueue:nil];

}

NSLog (@ "queue complete");

}

Requestfinished: The method is to request the object successful callback method, so there are two request object it will be called two times, in the line ① code we according to the Go button click the event set the Tag property of the Request object, to determine which request object callback. It is then loaded to show different picture views. The ② code [_networkqueue Requestscount] can determine the number of request objects in the queue.

Other...

[Networkqueueforsong Reset];

Download Queue Agent Method

[Networkqueueforsong setrequestdidfailselector: @selector (singledownloadfail:)];

[Networkqueueforsong setrequestdidfinishselector: @selector (singledownloadfinished:)];

[Networkqueueforsong setrequestdidreceiveresponseheadersselector: @selector (downloadreceiveresponseheader:)];

[Networkqueueforsong setrequestdidstartselector: @selector (Singledownloadstart:)];

[Networkqueueforsong setqueuedidfinishselector: @selector (downloadfinish)];

[Networkqueueforsong setdelegate:self];

Set the download queue properties, set to 1 only allow the next end to the next one, the default is the parallel download no points before and after

[Networkqueueforsong setmaxconcurrentoperationcount:1];

Asinetworkqueue Queue Download

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.