Note that there are some explanatory texts on larger screens.

plurals
  1. POAFNetworking + queue + cancel operations + junk files
    primarykey
    data
    text
    <p>i 'm downloading some files using AFNetworking using a queue. Here is my code:</p> <pre><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:&amp;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 &gt; 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]; } </code></pre> <p>the code is working fine on file downloading but i want some cancel functionality so i have a button with an action that has the code below:</p> <pre><code>[apiClient.operationQueue cancelAllOperations]; </code></pre> <p>the operations cancel file but then there are some junk files on the Documents folder. By saying junk i mean file that started downloading i canceled them and the i get a file with the same name but useless can't be opened cause it's damaged.</p> <p>How can i prevent AF from doing that and keep only the completed files when i cancel it?</p> <p>any help would be grateful.</p> <p>Also tried canceling job by job like that:</p> <pre><code>for (AFHTTPRequestOperation *ap in apiClient.operationQueue.operations) { [ap cancel]; NSLog(@"%@",ap.responseFilePath); NSFileManager *fileManager = [NSFileManager defaultManager]; [fileManager removeItemAtPath:ap.responseFilePath error:nil]; } </code></pre> <p>and deleting the junk files but that doesn't work either.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload