【问题标题】:AFNetworking + queue + cancel operations + junk filesAFNetworking + 队列 + 取消操作 + 垃圾文件
【发布时间】:2013-02-26 20:05:12
【问题描述】:

我正在使用 AFNetworking 使用队列下载一些文件。这是我的代码:

apiClient =[[AFHTTPClient alloc]initWithBaseURL: [NSURL URLWithString:ZFREMOTEHOST]];
    for (NSString *element in self.productsArray) {
            NSURL *url = [NSURL URLWithString:element];
            NSURLRequest *request = [NSURLRequest requestWithURL:url];

            op = [[AFHTTPRequestOperation alloc] initWithRequest:request];

            NSString *documentsDirectory = nil;
            NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
            documentsDirectory = [paths objectAtIndex:0];

            NSString *targetFilename = [url lastPathComponent];
            NSString *targetPath = [documentsDirectory stringByAppendingPathComponent:targetFilename];

            op.outputStream = [NSOutputStream outputStreamToFileAtPath:targetPath append:NO];

            [op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

            } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                //failure case
                NSLog(@"BaaZ  File NOT Saved %@", targetPath);

                //remove the file if saved a part of it!
                NSFileManager *fileManager = [NSFileManager defaultManager];
                [fileManager removeItemAtPath:targetPath error:&error];

                if (error) {
                    NSLog(@"error dude");
                }

                if ([operation isCancelled]) {
                    //that doesn't work.
                    NSLog(@"Canceled");
                }
            }];

            [op setDownloadProgressBlock:^(NSInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
                if (totalBytesExpectedToRead > 0) {
                    dispatch_async(dispatch_get_main_queue(), ^{
                        self.progressView.alpha = 1;
                        self.progressView.progress = (float)totalBytesRead / (float)totalBytesExpectedToRead;
                        NSString *label = [NSString stringWithFormat:@"Downloaded %lld of %lld bytes", totalBytesRead,totalBytesExpectedToRead];
                        self.progressLabel.text = label;
                    });
                }
            }];
        [self.resourcesArray addObject:op];

        }
    for (AFHTTPRequestOperation *zabols in self.resourcesArray) {
        [apiClient.operationQueue addOperation:zabols];
    }

代码在文件下载时运行良好,但我想要一些取消功能,所以我有一个按钮,其中包含以下代码:

[apiClient.operationQueue cancelAllOperations];

操作取消​​文件,但在 Documents 文件夹中有一些垃圾文件。说垃圾是指开始下载的文件我取消了它们,我得到了一个同名但无用的文件,因为它已损坏而无法打开。

我怎样才能防止 AF 这样做并在我取消它时只保留已完成的文件?

任何帮助将不胜感激。

还尝试像这样逐个取消工作:

for (AFHTTPRequestOperation *ap in apiClient.operationQueue.operations) {
        [ap cancel];
        NSLog(@"%@",ap.responseFilePath);
        NSFileManager *fileManager = [NSFileManager defaultManager];
        [fileManager removeItemAtPath:ap.responseFilePath error:nil];

    }

并删除垃圾文件,但这也不起作用。

【问题讨论】:

  • 您希望删除垃圾文件或完全下载的文件不损坏?
  • 是的,我也尝试过使用 isFinished 属性,但即使我取消了它们,它也会继续下载它们!

标签: objective-c ios download afnetworking


【解决方案1】:

尝试使用AFDownloadRequestOperation 扩展,它还支持恢复部分下载,使用临时目录并有一个特殊的块来帮助计算正确的下载进度。

【讨论】:

    【解决方案2】:

    你也可以试试:github

    【讨论】:

    • 代码太大,无法在此处添加,可能会对某些人有所帮助,这就是将其添加到此处的原因!你有什么建议?
    猜你喜欢
    • 1970-01-01
    • 2013-04-16
    • 1970-01-01
    • 2022-06-14
    • 1970-01-01
    • 2012-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多