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.handDownloadOperation.mto your project and include the header file in your .m file:#import "DownloadOperation.h"Create a
NSOperationQueuefor 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
Downloadoperation, downloading fromurl, saving the file topath. -
– initWithURL:Initialize
Downloadoperation, downloading fromurl, saving the file to documents folder, using the URL’slastPathComponentas the filename.
Properties
-
urlThe remote URL of the file being downloaded.
property -
pathThe local path of the file being downloaded.
property -
downloadCompletionBlockDownload completion block
property -
downloadProgressBlockDownload progress block
property -
expectedContentLengthExpected content length as reported by server
property -
progressContentLengthProgress content length as determined by bytes received.
property
Properties
downloadCompletionBlock
Download completion block
@property (nonatomic, copy) DownloadCompletionBlock downloadCompletionBlockDeclared In
DownloadOperation.hdownloadProgressBlock
Download progress block
@property (nonatomic, copy) DownloadProgressBlock downloadProgressBlockDeclared In
DownloadOperation.hexpectedContentLength
Expected content length as reported by server
@property long long expectedContentLengthDiscussion
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.hpath
The local path of the file being downloaded.
@property (nonatomic, copy) NSString *pathDiscussion
Generally not set manually, but rather by call to initWithURL:path:.
See Also
Declared In
DownloadOperation.hprogressContentLength
Progress content length as determined by bytes received.
@property long long progressContentLengthDiscussion
This is how many bytes received thus far.
Declared In
DownloadOperation.hInstance 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 *)urlParameters
- 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.hinitWithURL:path:
Initialize Download operation, downloading from url, saving the file to path.
- (id)initWithURL:(NSURL *)url path:(NSString *)pathParameters
- 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 thelastPathComponentof the URL, and will be stored in theDocumentsfolder.
Return Value
Download operation
Discussion
The operation starts when this operation is added to an operation queue.
See Also
Declared In
DownloadOperation.h