キューを使用してAFNetworkingを使用していくつかのファイルをダウンロードしています。ここに私のコードは次のとおりです。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];
操作がキャンセルをファイルにはいくつかの迷惑ファイルがあります。ジャンクとは、ダウンロードを開始したファイルをキャンセルしたことを意味し、同じ名前のファイルを取得しますが、破損しているため開けません。
どのように私はそれを行うのを防ぐことができますし、私はそれをキャンセルすると完了したファイルだけを維持する?
ご協力いただければ幸いです。
for (AFHTTPRequestOperation *ap in apiClient.operationQueue.operations) {
[ap cancel];
NSLog(@"%@",ap.responseFilePath);
NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager removeItemAtPath:ap.responseFilePath error:nil];
}
とジャンクファイルを削除するが、それはどちらか動作しません:
はまた、そのような仕事によって、ジョブをキャンセルしようとしました。
あなたはジャンクを削除したいか、完全にダウンロードされたファイルが破損していませんか? –
はいまさに私もisFinishedプロパティで試してみましたが、キャンセルしてもそれをダウンロードし続けました! – zaabalonso