在UIViewController中回调块

时间:2016-07-15 13:34:06

标签: ios objective-c nsurlconnection objective-c-blocks

我有一个查询。我想异步执行一些操作,比如在UIViewController内从互联网上下载一个大文件。 我在委托中写了一个方法,它接受请求和阻塞作为参数

-(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);

 }];

}

我的viewDidLoad方法看起来像这样

- (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.}

现在有一个弹出视图控制器的后退按钮

- (IBAction)BackBtnClicked:(id)sender{
[self.navigationController popViewControllerAnimated:true];}

那么如何检测我是否已从completionHandler中的内存中弹出控制器?或者如何从执行中取消completionHandler。 因为如果我不这样做会有泄漏。

0 个答案:

没有答案