iOS Development: ASIHTTPREQUEST Progress tracking

Source: Internet
Author: User
Tags bug id

This article introduces you to the asihttprequest progress tracking, which includes tracking the download progress of individual request, tracking the download progress of a series of request, tracking the upload progress of a single request, tracking the upload progress of a series of request, Accurate progress bar vs simple progress bar, custom progress tracking, etc. content.

Each asihttprequest has two delegate to track progress:

Downloadprogressdelegate (Downloads) and Uploadprogressdelegate (uploaded).

Progress delegate can be nsprogressindicators (Mac OS X) or Uiprogressviews (IPhone). ASIHTTPRequest will adapt to these two class behaviors. You can also use a custom class as a progress delegate as long as it responds to setprogress: functions.

If you execute a single request, you need to set upload/download progress for the request delegate

If you are making multiple requests and you want to track the progress of the entire queue, you must use Asinetworkqueue and set the progress of the queue delegate

If the above two you want to have at the same time, congratulations, 0.97 version after the asihttprequest, this can have ^ ^

IMPORTANT: If you upload data to a website that requires authentication, the upload progress bar will be reset to the previous progress value if each authorization fails. Therefore, when interacting with a Web server that requires authorization, it is recommended that you use the upload progress bar only when Usesessionpersistence is yes, and that you use a different request for authorization before you track the upload progress of a large amount of data.

Tracking the data upload progress less than 128KB is currently not possible, and for data larger than 128KB, progress delegate will not receive the first 128KB block of progress information. This is because of the limitations of the Cfnetwork Library API. We have submitted bug reports (Bug ID 6596016) to Apple, and we hope that Apple will be able to modify the Cfnetwork library to achieve these functions.

2009-6-21:apple's buddies are awesome.!iphone 3.0 SDK, the buffer size has been reduced to 32KB, our upload progress bar can be more accurate.

Track download progress for a single request

In this case, Myprogressindicator is a nsprogressindicator.

ASIHTTPRequest *request = [ASIHTTPRequest Requestwithurl:url];

[Request Setdownloadprogressdelegate:myprogressindicator];

[Request startsynchronous];

NSLog (@ "Max:%f, Value:%f", [Myprogressindicator maxvalue],[myprogressindicator doublevalue]);

Track download progress for a series of request

In this case, Myprogressindicator is a uiprogressview, Myqueue is a asinetworkqueue.

-(void) Fetchthisurlfivetimes: (nsurl *) URL

{

[Myqueue cancelalloperations];

[Myqueue Setdownloadprogressdelegate:myprogressindicator];

[Myqueue setdelegate:self];

[Myqueue setrequestdidfinishselector: @selector (queuecomplete:)];

int i;

For (i=0 i<5; i++) {

ASIHTTPRequest *request = [ASIHTTPRequest Requestwithurl:url];

[Myqueue Addoperation:request];

}

[Myqueue go];

}

-(void) Queuecomplete: (Asinetworkqueue *) queue

{

NSLog (@ "Value:%f", [myprogressindicator progress]);

}

In this example, we have called [Myqueue go] for asinetworkqueues.

Track the upload progress of a single request

In this case, Myprogressindicator is a uiprogressview.

Asiformdatarequest *request = [Asiformdatarequest Requestwithurl:url];

[Request setpostvalue:@ "Ben" forkey:@ "first_name"];

[Request setpostvalue:@ "Copsey" forkey:@ "last_name"];

[Request Setuploadprogressdelegate:myprogressindicator];

[Request startsynchronous];

NSLog (@ "Value:%f", [myprogressindicator progress]);

Tracking the upload progress of a series of request

In this case, Myprogressindicator is a nsprogressindicator, and Myqueue is a asinetworkqueue.

-(void) Uploadsomethingfivetimes: (nsurl *) URL

{

[Myqueue cancelalloperations];

[Myqueue Setuploadprogressdelegate:myprogressindicator];

[Myqueue setdelegate:self];

[Myqueue setrequestdidfinishselector: @selector (queuecomplete:)];

int i;

For (i=0 i<5; i++) {

ASIHTTPRequest *request = [Asiformdatarequest Requestwithurl:url];

[Request setpostbody:[@ "Some data" datausingencoding:nsutf8stringencoding]];

[Myqueue Addoperation:request];

}

[Myqueue go];

}

-(void) Queuecomplete: (Asinetworkqueue *) queue

{

NSLog (@ "Max:%f, Value:%f", [Myprogressindicator maxvalue],[myprogressindicator doublevalue]);

}

Precision progress bar vs simple progress bar

ASIHTTPRequest provides two kinds of progress bar display, simple progress bar and accurate progress bar, using asihttprequests and Asinetworkqueues showaccurateprogress to control. Setting showaccurateprogress for a request will only be valid for the request. If you set up a showaccurateprogress for a queue, it will affect all the request in the queue.

Simple progress bar

When you use a simple progress bar, the progress bar is only updated when a request completes. For a single request, this means that you have only two progress status: 0% and 100%. For a queue with 5 request, there are five states: 0%,25%,50%,75%,100%, and when each request completes, the progress bar grows once.

The Simple progress bar (showaccurateprogress = NO) is the default value for Asinetworkqueue, and is suitable for a large number of small data requests.

Accurate progress bar

When the exact progress bar is used, the progress bar is updated whenever the byte is uploaded or downloaded. It applies to uploading/downloading large chunks of data, and better displays the amount of data that has been sent/received.

Tracking uploads with an accurate progress bar can reduce the efficiency of the interface slightly, because progress delegate (typically uiprogressviews or nsprogressindicators) are redrawn more frequently.

Tracking downloads with an accurate progress bar can affect the efficiency of the interface more, because queues first head requests for each get type request to count the total amount of downloads. It is highly recommended that you use precise progress bars for queues that download large files, but avoid using precise progress bars for large numbers of small data requests.

The exact progress bar (showaccurateprogress = yes) is the default value for ASIHTTPRequest performed synchronously.

Custom Progress Tracking

The Asiprogressdelegate protocol defines all the methods that can update a request's progress. In most cases, it would be nice to set your uploadprogressdelegate or downloadprogressdelegate for Nsprogressindicator or Uiprogressview. However, if you want more complex tracking, your progress delegate implement the following functions than Setprogress: (IOS) or setdoublevalue:/setmaxvalue: (MAC) Good:

These functions allow you to update your progress when the actual amount of data is uploaded or downloaded, rather than a number between 0 and 1 of the simple method.

Downloadprogressdelegates method

Request:didreceivebytes: This function is invoked each time the request downloads more data (note that this function is different from the general proxy implementation of Request:didreceivedata: function).

Request:incrementdownloadsizeby: When the size of the download changes, the function is invoked, and the incoming argument is the size you need to increase. This usually occurs when the request receives a response header and finds the download size.

Uploadprogressdelegates method

Request:didsendbytes: This function is invoked each time the request can send more data. Note: When a request needs to eliminate the upload progress (usually the request sends a piece of data, but because the authorization fails or any other reason that the data needs to be resend) The function is passed into a number less than 0.

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.