使用系统的某些block api(如UIView的block版本写动画时),是否也考虑循环引用问题?...

  • 系统的某些block api中,UIView的block版本写动画时不需要考虑,但也有一些api 需要考虑
  • 以下这些使用方式不会引起循环引用的问题
[UIView animateWithDuration:duration animations:^
{ [self.superview layoutIfNeeded]; }];

[[NSOperationQueue mainQueue] addOperationWithBlock:^
{ self.someProperty = xyz; }]; [[NSNotificationCenter defaultCenter] addObserverForName:@"someNotification" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * notification) { self.someProperty = xyz; }]; 
  • 但如果方法中的一些参数是 成员变量,那么可以造成循环引用,如 GCD 、NSNotificationCenter调用就要小心一点,比如 GCD 内部如果引用了 self,而且 GCD 的参数是 成员变量,则要考虑到循环引用,举例如下:

    • GCD

      • 分析:self-->_operationsQueue-->block-->self形成闭环,就造成了循环引用
        __weak __typeof__(self) weakSelf = self;
        dispatch_group_async(_operationsGroup, _operationsQueue, ^
        {
        [weakSelf doSomething];
        [weakSelf doSomethingElse];
        } );
        
    • NSNotificationCenter

      • 分析:self-->_observer-->block-->self形成闭环,就造成了循环引用
      __weak __typeof__(self) weakSelf = self;
      _observer = [[NSNotificationCenter defaultCenter]
      addObserverForName:@"testKey" object:nil queue:nil usingBlock:^(NSNotification *note){ [weakSelf dismissModalViewControllerAnimated:YES]; }];

转载于:https://www.cnblogs.com/huangzs/p/7516715.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值