报摊:放弃资产下载仍在下载

时间:2012-11-01 06:00:58

标签: ios objective-c download assets newsstand-kit

我正在制作报刊亭应用,每个问题都有很多下载资源需要下载。 “issues”是服务器请求的NSArray *。 我开始下载迭代MyDownloader类中的所有资产:

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];
}

我正在存储localFilepath以供以后在连接中使用:didFinishDownloading:destinationURL方法。

一切都很好,除了一件事。这是我在应用程序中添加的代码:didFinishLaunchingWithOptions:method

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

这也很好。但是当我需要取消所有排队的下载时,我会从应用程序注释掉之前的代码:didFinishLaunchingWithOptions:,我得到这样的日志消息:

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}"

我希望所有下载都被取消。但是当我查看应用程序的Library / Cache目录时,我看到许多文件以“bgdl-2896-”开头的文件名下载,依此类推。所以它们没有被取消,它们被NewsstandKit下载。 connection:didFinishDownloading:destinationURL方法也没有被调用。这就是麻烦 - 资产消耗设备上的互联网流量和存储空间。

如何强制取消资产下载我不再需要了?

1 个答案:

答案 0 :(得分:0)

不是为每个问题下载这么多资产,而是将资产存档到一个zip文件夹中,然后下载与您的问题相对应的zip文件。

这样,每个要下载的问题只有一个文件,其中包含该问题所需的所有资产。然后,您可以解压缩并拥有对资产的访问权限。通过这种方式,您可以轻松整理问题及其资产。

希望这会对你有所帮助。