Tôi đang tải xuống một số tệp bằng cách sử dụng AFNetworking bằng hàng đợi. Đây là mã của tôi:AFNetworking + queue + hủy thao tác + tập tin rác
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];
}
mã đang làm việc tốt trong hồ sơ tải nhưng tôi muốn có một số hủy bỏ chức năng vì vậy tôi có một nút với một hành động mà có mã dưới đây:
[apiClient.operationQueue cancelAllOperations];
các hoạt động hủy nhưng sau đó có một số tệp rác trên thư mục Tài liệu. Bằng cách nói junk tôi có nghĩa là tập tin mà bắt đầu tải về tôi bị hủy bỏ chúng và tôi nhận được một tập tin có cùng tên nhưng vô dụng không thể mở được vì nó bị hư hỏng.
Làm cách nào để ngăn không cho AF thực hiện điều đó và chỉ giữ lại các tệp hoàn chỉnh khi tôi hủy?
bất kỳ trợ giúp nào sẽ biết ơn.
Cũng đã cố gắng hủy bỏ việc bởi công việc như thế:
for (AFHTTPRequestOperation *ap in apiClient.operationQueue.operations) {
[ap cancel];
NSLog(@"%@",ap.responseFilePath);
NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager removeItemAtPath:ap.responseFilePath error:nil];
}
và xóa các tập tin rác nhưng điều đó không làm việc, hoặc.
bạn muốn các tệp bị xóa hoặc tải xuống hoàn toàn không bị hỏng? –
có chính xác tôi cũng đã thử với tài sản isFinished nhưng nó giữ tải chúng ngay cả khi tôi đã hủy bỏ chúng! – zaabalonso