IOS development: ASIHTTPRequest progress tracking

Source: Internet
Author: User
Tags bug id

IOS development: ASIHTTPRequest progress tracking

This article describes the progress tracking of ASIHTTPRequest, including tracking the download progress of a single request, tracking the download progress of a series of requests, and tracking the upload progress of a single request, tracking the upload progress of a series of requests, precise progress bar vs simple progress bar, custom progress tracking, and so on.

Each ASIHTTPRequest has two delegate entries to track the progress:

DownloadProgressDelegate (download) and uploadProgressDelegate (upload ).

The progress delegate can be NSProgressIndicators (Mac OS X) or UIProgressViews (iPhone). ASIHTTPRequest will adapt to the behavior of the two classes. You can also use a custom class as the progress delegate, as long as it responds to the setProgress: function.

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

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

If you want to have both of the two, congratulations, for ASIHTTPRequest after version 0.97, this can be ^

IMPORTANT: If you upload data to a website that requires authentication, the upload progress bar will be reset to the previous progress value every time the authorization fails. Therefore, when interacting with the web server that requires authorization, we recommend that you use the upload progress bar only when useSessionPersistence is set to YES, and make sure that you are tracking the upload progress of a large amount of data, use another request for authorization first.

The progress of Data uploading with a value smaller than kb cannot be tracked. For data with a value greater than KB, the Progress delegate will not receive the progress information of the first KB data block. This is because of the CFNetwork library API restrictions. We submitted a bug report (bug id 6596016) to apple, hoping apple could modify the CFNetwork library to implement the above features.

: Apple's friends are awesome! In the iPhone 3.0 SDK, the buffer size has been reduced to 32 KB, and our upload progress bar can be more accurate.

Track the download progress of a single request

In this example, myProgressIndicator is an NSProgressIndicator.

ASIHTTPRequest * request = [ASIHTTPRequest requestWithURL: url];

[Request setDownloadProgressDelegate: myProgressIndicator];

[Request startSynchronous];

NSLog (@ "Max: % f, Value: % f", [myProgressIndicator maxValue], [myProgressIndicator doubleValue]);

Tracking the download progress of a series of requests

In this example, myProgressIndicator is a UIProgressView and myQueue is an 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 already called [myQueue go] For ASINetworkQueues.

Track the upload progress of a single request

In this example, 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 requests

In this example, myProgressIndicator is an NSProgressIndicator and myQueue is an 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]);

}

Precise progress bar vs simple progress bar

ASIHTTPRequest provides two types of progress bars: simple progress bars and precise progress bars, which are controlled by showAccurateProgress of ASIHTTPRequests and ASINetworkQueues. Setting showAccurateProgress for a request is only valid for this request. If you set showAccurateProgress for a queue, all requests in the queue will be affected.

Simple progress bar

When a simple progress bar is used, the progress bar is updated only when one request is complete. For a single request, this means that you only have two Progress states: 0% and 100%. For a queue with five requests, there are five statuses: 0%, 25%, 50%, 75%, 100%. When each request is complete, the progress bar increases once.

The simple progress bar (showAccurateProgress = NO) is the default value of ASINetworkQueue and is suitable for a large number of small data requests.

Precise progress bar

When a precise progress bar is used, the progress bar is updated whenever bytes are uploaded or downloaded. It is suitable for uploading/downloading large data blocks, and better displays the data volumes that have been sent/received.

Using the precise progress bar to track uploads will slightly reduce the interface efficiency, because the progress of delegate (generally UIProgressViews or NSProgressIndicators) will be re-painted more frequently.

Tracking download with a precise progress bar will affect the interface efficiency, because the queue will first make HEAD requests for each GET request to count the total downloads. We strongly recommend that you use a precise progress bar for the queue for downloading large files, but avoid using a precise progress bar for a large number of small data requests.

The precise progress bar (showAccurateProgress = YES) is the default value of ASIHTTPRequest executed in synchronous mode.

Custom progress tracking

The ASIProgressDelegate Protocol defines all methods for updating the request progress. In most cases, setting your uploadProgressDelegate or downloadProgressDelegate as NSProgressIndicator or UIProgressView is fine. However, if you want to perform more complex tracing, the following functions are better than setProgress: (iOS) or setDoubleValue:/setMaxValue: (Mac) in your progress delegate:

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

DownloadProgressDelegates Method

Request: didReceiveBytes: This function is called every time more data is downloaded from a request. (Note that this function is different from the request: didReceiveData: function implemented by a common proxy ).

Request: incrementDownloadSizeBy: When the download size changes, this function will be called. The input parameter is the size you need to increase. This usually happens when the request receives the response header and finds the download size.

UploadProgressDelegates Method

Request: didSendBytes: This function is called every time a request can send more data. Note: When a request needs to cancel the upload progress (this request usually sends a piece of data, but this data needs to be re-sent due to Authorization failure or other reasons) this function is passed in a number smaller than zero.

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.