Objective c AFX2B;队列+;取消操作+;垃圾文件

Objective c AFX2B;队列+;取消操作+;垃圾文件,objective-c,ios,download,afnetworking,Objective C,Ios,Download,Afnetworking,我正在使用队列使用AFNetworking下载一些文件。这是我的密码: apiClient =[[AFHTTPClient alloc]initWithBaseURL: [NSURL URLWithString:ZFREMOTEHOST]]; for (NSString *element in self.productsArray) { NSURL *url = [NSURL URLWithString:element]; NSURLReq

我正在使用队列使用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];
操作会取消文件,但文档文件夹中会有一些垃圾文件。我说的垃圾是指开始下载的文件,我取消了它们,我得到了一个同名但无用的文件,因为它已损坏,无法打开

如何防止AF执行此操作,并在取消时仅保留已完成的文件

任何帮助都将不胜感激

还尝试按如下方式逐个取消作业:

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

    }

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

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

您也可以尝试:

您希望删除垃圾或完全下载的文件不受损坏吗?是的,确实如此,我也尝试了isFinished属性,但即使我取消了它们,它仍会继续下载它们!代码太大,无法在此处添加,可能会帮助某些人,这就是为什么在此处添加它!你有什么建议?