【问题标题】:Correct way to access super in completion block在完成块中访问超级的正确方法
【发布时间】:2013-11-28 00:01:24
【问题描述】:

我有一个 NSOperation 的子类,它通过网络发送取消请求。 我只想在请求成功时取消操作:

// NSOperation 的覆盖取消 -(无效)取消{ [NSURLConnection 发送异步请求:请求 队列:[NSOperationQueue 主队列] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { [超级取消]; }]; }

这会导致任何保留周期或其他什么吗?我需要弱超吗(我正在使用 ARC)

【问题讨论】:

    标签: ios objective-c automatic-ref-counting objective-c-blocks retain-cycle


    【解决方案1】:

    它不会创建保留循环,您的实例没有对完成块的强引用。但是我不希望像这样调用super 实现,也许是这样的

    -(void)cancelAfterRequest 
    {
        [NSURLConnection sendAsynchronousRequest:request 
                                           queue:[NSOperationQueue mainQueue]
                               completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
             [self cancel];
        }];
    }
    

    【讨论】:

      猜你喜欢
      • 2019-02-11
      • 2016-04-16
      • 2016-07-12
      • 1970-01-01
      • 2011-03-24
      • 2022-01-10
      • 1970-01-01
      • 2020-03-28
      • 2013-12-27
      相关资源
      最近更新 更多