I'm downloading the image using AFHttpRequestOperation, i want to cancel this operation on a button click.How do i do this
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:strImagePath]];
self.managerDownloadActualImage = [AFHTTPRequestOperationManager manager];
self.managerDownloadActualImage.responseSerializer = [AFHTTPResponseSerializer serializer];
self.operationImageDownload = [self.managerDownloadActualImage HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Download succeeded.");
chatData.isDownloaded = [NSNumber numberWithBool:YES];
chatData.isThumbDownloaded = [NSNumber numberWithBool:YES];
NSData *responseData = (NSData *)responseObject;
chatData.actual_path = [NSString stringWithFormat:@"%@",[self saveImageInDocumemntDirectoryWithImageData:responseData]];
[self saveData];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Download failed with error: %@", error);
}];
[self.operationImageDownload setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
NSLog(@"Bytes read: %lu", (unsigned long)bytesRead);
NSLog(@"Total bytes read %lld", totalBytesRead);
NSLog(@"Total progress: %Lf", (long double)totalBytesRead / totalBytesExpectedToRead);
int currentPercentage = ((long double)totalBytesRead / totalBytesExpectedToRead) * 100;
if (currentPercentage <= 100) {
self.customProgressView.percent = currentPercentage;
[self.customProgressView setNeedsDisplay];
}
else {
[self.customProgressView removeFromSuperview];
}
}];
NSLog(@"Starting download.");
[self.operationImageDownload start];
i had used
[self.managerDownloadActualImage.operationQueue cancelAllOperations];
but this does not stop the download process.