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) {
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.
zaabalonso
source share