DownloadOperation Class Reference
Inherits from | NSOperation |
Declared in | DownloadOperation.h DownloadOperation.m |
Overview
Download Operation
This is a operation, subclassed from NSOperation
, performs a download of a file (done atomically) that can be added to a NSOperationQueue
.
Usage
Copy the
DownloadOperation.h
andDownloadOperation.m
to your project and include the header file in your .m file:#import "DownloadOperation.h"
Create a
NSOperationQueue
for the download operation:self.downloadQueue = [[NSOperationQueue alloc] init];
Give that operation queue some reasonable maximum operation count (enjoy concurrency, but staying clear of the maximum number of network operations that iOS supports:
self.downloadQueue.maxConcurrentOperationCount = 4;
Create a download operation:
DownloadOperation *downloadOperation = [[DownloadOperation alloc] initWithURL:url];
Optionally, provide that operation block completion and progress blocks:
downloadOperation.downloadCompletionBlock = ^(DownloadOperation *operation, BOOL success, NSError *error) { if (error) { NSLog(@"%s: downloadCompletionBlock error: %@", __FUNCTION__, error); } // do whatever else you want upon completion }; downloadOperation.downloadProgressBlock = ^(DownloadOperation *operation, long long progressContentLength, long long expectedContentLength) { CGFloat progress = (expectedContentLength > 0 ? (CGFloat) progressContentLength / (CGFloat) expectedContentLength : (progressContentLength % 1000000l) / 1000000.0f); [cell.downloadProgressView setProgress:progress animated:YES]; }
Add the operation to your operation queue to start the operation.
[self.downloadQueue addOperation:downloadOperation];
If you need to stop an operation, you can either cancel the individual operation:
[downloadOperation cancel];
Or you can stop all of the download operations:
[self.downloadQueue addOperation:downloadOperation];
Note: This operates atomically, downloading the file to a temporary file, and then moving the file to the final destination.
Warning: This will replace the file at the destination path.
Tasks
Initialization methods
-
– initWithURL:path:
Initialize
Download
operation, downloading fromurl
, saving the file topath
. -
– initWithURL:
Initialize
Download
operation, downloading fromurl
, saving the file to documents folder, using the URL’slastPathComponent
as the filename.
Properties
-
url
The remote URL of the file being downloaded.
property -
path
The local path of the file being downloaded.
property -
downloadCompletionBlock
Download completion block
property -
downloadProgressBlock
Download progress block
property -
expectedContentLength
Expected content length as reported by server
property -
progressContentLength
Progress content length as determined by bytes received.
property
Properties
downloadCompletionBlock
Download completion block
@property (nonatomic, copy) DownloadCompletionBlock downloadCompletionBlock
Declared In
DownloadOperation.h
downloadProgressBlock
Download progress block
@property (nonatomic, copy) DownloadProgressBlock downloadProgressBlock
Declared In
DownloadOperation.h
expectedContentLength
Expected content length as reported by server
@property long long expectedContentLength
Discussion
Please note that this is not reliable (most HTTP servers report it, some don’t, and in some rare cases, a server might not report an accurate number. This is used for estimation purposes only. Actual completion is determined by the operation’s completion, not by reaching the prescribed number of bytes.
Declared In
DownloadOperation.h
path
The local path of the file being downloaded.
@property (nonatomic, copy) NSString *path
Discussion
Generally not set manually, but rather by call to initWithURL:path:
.
See Also
Declared In
DownloadOperation.h
progressContentLength
Progress content length as determined by bytes received.
@property long long progressContentLength
Discussion
This is how many bytes received thus far.
Declared In
DownloadOperation.h
Instance Methods
initWithURL:
Initialize Download
operation, downloading from url
, saving the file to documents folder, using the URL’s lastPathComponent
as the filename.
- (id)initWithURL:(NSURL *)url
Parameters
- url
The remote URL of the file being downloaded.
Return Value
Download operation
Discussion
The operation starts when this operation is added to an operation queue.
See Also
Declared In
DownloadOperation.h
initWithURL:path:
Initialize Download
operation, downloading from url
, saving the file to path
.
- (id)initWithURL:(NSURL *)url path:(NSString *)path
Parameters
- url
The remote URL of the file being downloaded.
- path
The local filename of the file being downloaded. This should be the full path of the file (both the path and filename) and should be in a directory that the user has permission.
If this
nil
, the filename will be taken from thelastPathComponent
of the URL, and will be stored in theDocuments
folder.
Return Value
Download operation
Discussion
The operation starts when this operation is added to an operation queue.
See Also
Declared In
DownloadOperation.h