I have a query. I wanted to perform some operation asynchronously, say downloading a big file from internet inside UIViewController
.
I wrote a Method in delegate which takes request and block as parameter
-(void)downloadDataFromURL:(NSMutableURLRequest *)_request withCompletionHandler:(void (^)(NSData *, bool ))completionHandler{
[NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@""]] queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *_response, NSData *_data, NSError *_error)
{
completionHandler(_data, true);
}];
}
In my viewDidLoad
method looks like this
- (void)viewDidLoad{
mainDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[mainDelegate downloadDataFromURL:[NSMutableURLRequest requestWithURL:[NSURL URLWithString:@""]] withCompletionHandler:^(NSData *_data, bool isFinished)
{
//Processed only if we are inside memory. anyway there are many leaks when I load these class again
}];
[super viewDidLoad];
// Do any additional setup after loading the view.}
now there is back button which pop the view controller
- (IBAction)BackBtnClicked:(id)sender{
[self.navigationController popViewControllerAnimated:true];}
So how can I detect if I have popped the controller from memory inside my completionHandler? Or how can I cancel the completionHandler from getting executed. Because if I don't there will be leaks.