How to cancel AFdownloadrequestoperation and not save previous progress (delete temporary file) iOS

I am downloading a file from the server.
I can successfully upload the file. But whenever I cancel the download operation (in the interval) and restart the download. Download starts from the previous move (not from scratch). but I want to reload the boot from scratch.

Setup operation.deleteTempFileOnCancel = YES;also doesn't help

The file is not created on the specified destination path when I cancel the download between

operation.tempPath is returning null (not able to delete temporary file)

I am using the following code

     NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:model.downloadUrl]];
     operation = [[AFDownloadRequestOperation alloc] initWithRequest:request targetPath:path shouldResume:YES];
     operation.deleteTempFileOnCancel = YES;


            // download operation progressive block
            [operation setProgressiveDownloadProgressBlock:^(AFDownloadRequestOperation *operation, NSInteger bytesRead, long long totalBytesRead, long long totalBytesExpected, long long totalBytesReadForFile, long long totalBytesExpectedToReadForFile)
             {
                 // calculate current download progress

                 }
             }];

            // download operation completion block
            [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject)
             {


             } 

            failure:^(AFHTTPRequestOperation *operation, NSError *error) {

                 // operation failed


             }];

           [operation start];

I want to start the download from scratch if the download is canceled between them and then restarted. I am using iOS 7

+4
source share
1 answer

, . , - .

, tempPath

operation = [[AFDownloadRequestOperation alloc] initWithRequest:request targetPath:path shouldResume:YES];
NSLog("%@", operation.tempPath);

NSString *tempPath = _operation.tempPath;
[self.tempPathDict setObject:tempPath forKey:your_key];

,

NSString *tempPath = [self.tempPathDict objectForKey:your_key];
// Cancel the operation you want
[operation cancel];
// Remove the tempFile 
NSError *error = [[NSError alloc] init];
[[NSFileManager defaultManager] removeItemAtPath:tempPath error:&error];
// And last remove the path from the dictionary
[self.tempPathDict removeObjectForKey:your_key];
+1

Source: https://habr.com/ru/post/1540200/


All Articles