Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AFNetworking + queue + cancel operations + junk files

i 'm downloading 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 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:

[apiClient.operationQueue cancelAllOperations];

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.

How can i prevent AF from doing that and keep only the completed files when i cancel it?

any help would be grateful.

Also tried canceling job by job like that:

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

    }

and deleting the junk files but that doesn't work either.

like image 815
fizampou Avatar asked Nov 23 '12 07:11

fizampou


1 Answers

Try using AFDownloadRequestOperation extension, it additionally supports resume a partial download, uses a temporary directory and has a special block that helps with calculating the correct download progress.

like image 103
Ahmet Aygun Avatar answered Oct 13 '22 09:10

Ahmet Aygun