我正在製作一個報攤應用程序,我有許多下載資產可供下載。 「問題」是服務器請求的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:方法中
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下載的。連接:didFinishDownloading:destinationURL方法也不會被調用。這就是麻煩 - 資產會消耗設備上的互聯網流量和存儲空間。
我該如何強制取消資產下載我不再需要?
問題不在於資產數量,而在於即使在我想要時也不會取消。 – VasVF