AFNetworking + queue + cancellation operations + spam files

Download some files using AFNetworking using a queue. Here is my code:

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]; } 

The code works fine when the file is uploaded, but I need the undo function, so I have a button with an action that has the following code:

 [apiClient.operationQueue cancelAllOperations]; 

operation cancellation file, but then there are some junk files in the Documents folder. Speaking of junk, I mean the file that started the download, I canceled them, and I get the file with the same name, but it doesn't uselessly open because it is damaged.

How can I prevent AF from executing and save only completed files when I cancel it?

any help would be appreciated.

Also tried to cancel the task as follows:

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

and deleting unnecessary files, but this also does not work.

+7
source share
2 answers

Try using the AFDownloadRequestOperation extension, it also supports the resumption of partial downloads, uses a temporary directory and has a special block that helps in calculating the correct download progress.

+7
source

You can also try the following: github

-one
source

All Articles