0

I'm making a newsstand app, I have many download assets per issue to download. "issues" is a NSArray* requested from the server. I start downloading with iterating through all the assets like this in MyDownloader class:

for (int i=0; i< issues.count; i++)
    MyIssue *currentIssue = [issues objectAtIndex:i];
    NSString *filename = [currentIssue.remotePath lastPathComponent];
    NSString *localFilepath = [cache.cachePath stringByAppendingString:filename];

    //skip downloaded file
    if ([[NSFileManager defaultManager] fileExistsAtPath:localFilepath]) {
        continue;
    }

    NSURL *downloadURL = [NSURL URLWithString:currentIssue.remotePath];

    // let's create a request and the NKAssetDownload object
    NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:downloadURL];

    NKAssetDownload *assetDownload = [nkIssue addAssetWithRequest:req];
    [assetDownload setUserInfo:[NSDictionary dictionaryWithObjectsAndKeys:
                                localFilepath, @"Filepath",
                                nil]];
    // let's start download
    [assetDownload downloadWithDelegate:self];
}

I'm storing localFilepath for later use in connection:didFinishDownloading:destinationURL method.

Everything is working fine, except one thing. Here's the code I put in application:didFinishLaunchingWithOptions: method

NKLibrary *nkLib = [NKLibrary sharedLibrary];
for(NKAssetDownload *asset in [nkLib downloadingAssets]) {
    NSLog(@"Asset to download: %@",asset);
    MyDownloader *downloader = [MyDownloader sharedDownloader];
    [asset downloadWithDelegate:downloader];
}

This also works fine. But when I need to cancel all queued downloads, I comment out the previous code from application:didFinishLaunchingWithOptions:, I get log messages like this:

NewsstandKit: cleaning up abandoned asset downloads: (
"<NKAssetDownload: 0xa17ffe0> -> {identifier: '98E98868-0DD2-45FF-90B8-7CF80E02A952/B11F6C43-86CC-4434-ABC1-F4450FF163CF'  request: <NSMutableURLRequest http://servername/serverpath/file.zip>  downloading: NO}"

And I expect that all the downloads are cancelled. But when I look in the Library/Cache directory of an application, I see lots of files downloading with filenames starting with "bgdl-2896-" and so on. So they are not get cancelled, they are downloaded by the NewsstandKit. connection:didFinishDownloading:destinationURL method is also not called. That's the trouble - assets consumes internet traffic and storage on the device.

How can I force to cancel asset downloads I don't need any more?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
VasVF
  • 11
  • 2

1 Answers1

0

Rather than downloading so many assets for each issue, archive the assets into one zip folder and download the zip file corresponding to your issue.

This way you will have only one file for each issue to be downloaded which will have all the assets you need for that issue. Then you can unzip and have the access to your assets. This way it will be easy to organise your issues and their assets.

Hope this helps you.

Revanth
  • 113
  • 12
  • The problem is not about assets quantity, but about they are not cancelled even when I want to. – VasVF Mar 18 '14 at 09:27