zoukankan      html  css  js  c++  java
  • IOS如何解决强引用中的循环引用

    一个对象中强引用了block,在block中又强引用了该对象,就会发射循环引用。

    解决方法:

    1、

     __typeof (&*self) __weak weakSelf = self;
        [UIView animateWithDuration:_animationDuration animations:^{
            CGRect frame = weakSelf.menuView.frame;
            frame.origin.y = -40 - _separatorHeight;
            weakSelf.menuWrapperView.frame = frame;
        } completion:nil];

    2、

     __weak CCBluetoothDevice * weakSelf = self;
        
        [weakSelf.OTATimer invalidate];
        weakSelf.OTATimer = nil;

    3、

    id __block weakSelf = self;

    __weak id weakSelf = self;

    4、将其中一方强制制空 xxx = nil

    另外:

    __weak __typeof(self) weakSelf = self;
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^ {
            NSURL *portraitUrl = [NSURL URLWithString:strUrl];
            __block UIImage *protraitImg = [UIImage imageWithData:[NSData dataWithContentsOfURL:portraitUrl]];
            dispatch_sync(dispatch_get_main_queue(), ^{
                __strong __typeof(weakSelf) strongSelf = weakSelf;
                if (protraitImg!=nil) {
                    strongSelf.portraitImageView.image = protraitImg;
                }
            });
        });
  • 相关阅读:
    HashMap和Hashtable的区别
    装箱以及拆箱
    泛型
    LinkedList和ArrayList异同
    ArrayList的输出方式以及ArrayList的因子增长数
    limit的优化
    mysql五大数据引擎的分别
    ios
    css3(1)
    php
  • 原文地址:https://www.cnblogs.com/zhaosuning/p/9707805.html
Copyright © 2011-2022 走看看