3

Can anyone explain me how to cancel and retry an NKAssetDownload?

Currently I tried this:

NKAssetDownload * download = [issue.downloadingAssets objectAtIndex:0];
currentConnection = [download downloadWithDelegate:self];

To cancel the download I do:

 [currentConnection cancel];

This works, however the NKAssetDownload stays in the NKIssue downloadingAssets table. Next time I want to download the same issue I execute the same code again ( downloadWithDelegate ). However this doesn't do anything. It is as if the NKAssetDownload instance still uses the same NSURLConnection that I previously canceled so it doesn't launch the download. The real problem is that I cannot remove the NKAssetDownload from the NKIssue and therefore cannot create a new one pointing at the same URL to retry the download. As far as I can tell there is no way of canceling a download and retrying it later, other than deleting the NKIssue from the library (which throws away everything) and recreating it. Does anyone know how to do this?

There is a related question on S.O. Is it possible to cancel an NKAssetDownload? but the only solution provided there is to delete the NKIssue, and that is not what I want to do.

Community
  • 1
  • 1
Joris Mans
  • 6,024
  • 6
  • 42
  • 69
  • I have a [this](http://stackoverflow.com/questions/9615945/what-happened-after-nkissue-download-terminated-due-to-network-unavailability) question posted. – msk Aug 09 '12 at 19:07

2 Answers2

0

Newsstand download queues are maintained by Newsstand framework. There is no control given to developers to pause/cancel downloads (except to delete NKIssue from NKLibrary).

You can implement below method of NSURLConnectionDelegate to retry any failed download.

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error

I checked that if download fails and this method called nkIssue.downloadingAssets.count is always 0. You can add your issues's assets again and can start download.

If you want to pause NS download, I am afraid there is no way. If you want to cancel download only way is to delete NKIssue fron NKLibrary, which I think has no problem. You can always add NKIssue again and start download anytime later.

msk
  • 8,885
  • 6
  • 41
  • 72
  • What do you mean with "you can use didFailWithError"? How do I use that? I just want the user to be able to cancel the download. – Joris Mans Aug 10 '12 at 08:52
0

I think is better to "reset" the issue. because the issue state is no updated when cancelling the NSURLConnection and is stays forever "downloading". And you also won't need a pointer to the NSURLConnection.

Note: This will delete any completed downloads related to this issue.

-(NKIssue*) resetIssue:(NKIssue*)issue{
    NSString* issueName = [issue name];
    NSDate* issueDate = [issue date];
    NKLibrary * lib = [NKLibrary sharedLibrary];
    [lib removeIssue:issue];
    return [lib addIssueWithName:issueName date:issueDate];
}
user2387149
  • 1,219
  • 16
  • 28