开发者

Download mutiple files in background thread

I am making an iPad app where you can download files (like PDF, doc, etc) and view them offline.

I already have the view part and you can download a file to the document directory. As it is now you need to wait for the download to be finished to move on. This can be solved by putting it in a thread, but what happens when the u开发者_如何转开发ser downloads multiple files or even download the same file multiple times?

My idea is to make a download queue, with a view for the progress.

Workflow:

  • The user opens a document and press download, the user gets a message that the download is started and can be viewed in the offline documents view.

  • The user downloads 3 more documents.

  • When the user goes to the offline document view the user sees a table view with 4 filled cells. 2 documents are done loading and 2 other are still downloading because there is a download/status bar shown in the table view cell.

  • The downloaded documents can be viewed or deleted.

  • The downloads in progress can not be watched (yet) but can be cancelled.

I want to make a threaded download class where you can add urls to be downloaded. the class has methods to cancel and delete document-downloads, but also has methods to return the progress. If possible the class can handle simultaneous downloads.

The problem is, I don't know where to start?


NSURLConnection is already asynchronous. All you need to do is to create NSURLConnection instances, associate them with your data structures, and have at it.

Here's an example where I assume you have one UIView per item. If you use a table view you can't count on view instances, but instead associate a download with an NSIndexPath, or something else.

@implementation MyDownloadView
- (void)startDownload {
    NSMutableURLRequest *req = [[NSMutableURLRequest alloc] initWithURL:myURL cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10];
    [req setHTTPMethod:@"GET"];
    // Set headers etc. if you need
    [[[NSURLConnection alloc] initWithRequest:req delegate:self] autorelease];
    [req release];

    self.responseData = [[NSMutableData alloc] init];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    // Alternatively, store to a file so you don't run out of memory
    [self.responseData appendData:data];
}
@end

Then implement the other NSURLConnection delegate methods to do what you need.


I've written an open source example that has pretty much all features you want, canceling a download is currently only available in code, but it's pretty easy to add a button for that.

I'm using asi-http-request for managing the downloads, and they are displayed in a grid view (AQGridView) instead of a UITableView, but i think you get the idea.

Download progress is managed via KVO.

See PSPDFDownload.m for a start. Download the full demo here

Full disclosure: This demo uses PSPDFKit for faster pdf display. But the Kiosk example is exactly what you need, and you don't need to use PSPDFKit for pdf display. There's even an example code path that uses Apple's QuickLook.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜