【问题标题】:newsstand: abandoned asset downloads still downloading报亭:废弃资产下载仍在下载
【发布时间】: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


    【解决方案1】:

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

    这样,每个要下载的问题只有一个文件,其中包含该问题所需的所有资产。然后您可以解压缩并访问您的资产。这样可以轻松组织您的问题及其资产。

    希望对你有所帮助。

    【讨论】:

    • 问题不在于资产数量,而在于即使我想取消也不会取消。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多