【发布时间】:2017-09-08 20:12:18
【问题描述】:
我正在制作一个报亭应用程序,我每期都有许多下载资源可供下载。 “问题”是从服务器请求的 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 以供以后在 connection:didFinishDownloading:destinationURL 方法中使用。
一切正常,除了一件事。这是我在应用程序中输入的代码:didFinishLaunchingWithOptions: 方法
NKLibrary *nkLib = [NKLibrary sharedLibrary];
for(NKAssetDownload *asset in [nkLib downloadingAssets]) {
NSLog(@"Asset to download: %@",asset);
MyDownloader *downloader = [MyDownloader sharedDownloader];
[asset downloadWithDelegate:downloader];
}
这也很好用。但是当我需要取消所有排队的下载时,我会从 application: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 方法也没有被调用。这就是问题所在 - 资产会消耗设备上的互联网流量和存储空间。
如何强制取消我不再需要的资产下载?
【问题讨论】:
标签: ios objective-c download assets newsstand-kit