iOS开发-多层嵌套block中如何使用__weak和__strong

一、关于__weak
__weak只能在ARC模式下使用,也只能修饰对象(好比NSString等),不能修饰基本数据类型(好比int等)
__weak修饰的对象在block中不能够被从新赋值。
__weak只在ARC下使用,能够避免循环引用。
__weak修饰对象不会增长引用函数

    __weak __typeof(self) weakSelf = self;spa

    self.testBlock = ^{.net

        [weakSelf doSomeThing];指针

    });对象


弱引用不会影响对象的释放,可是当对象被释放时,全部指向它的弱引用都会自定被置为 nil,这样能够防止野指针。blog

二、关于__block
__block无论是ARC仍是MRC模式下均可以使用,能够修饰对象,还能够修饰基本数据类型。
在MRC下使用__block是能够避免循环引用的;
在ARC下使用 __block typeof(self)weakSelf = self;由于block是用过添加引用来访问实例变量的,因此self会被retain一次,block也是一个强引用,会引发循环引用。生命周期

__block修饰对象会增长引用get

三、关于 __strongit

- (void)viewDidLoad {table

    [super viewDidLoad];

    

    MyOBJ *mm = [[MyOBJ alloc]init];

    mm.name = @"Lilei";

    __weak typeof(student) weakSelf = mm;

    

    mm.doBlock = ^{

        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

            NSLog(@"my name is = %@",weakSelf.name);

        });

    };

    

    mm.doBlock();

}

//输出:my name is = (null)


在dispatch_after这个函数里面。在doBlock()的block结束以后,mm被自动释放了。
又因为dispatch_after里面捕获的__weak的mm,在原对象释放以后,__weak对象就会变成nil,防止野指针。

那么咱们怎么才能在weakSelf以后,block里面还能继续使用weakSelf以后的对象呢?

究其根本缘由就是weakSelf以后,没法控制何时会被释放,为了保证在block内不会被释放,须要添加__strong。

在block里面使用的__strong修饰的weakSelf是为了在函数生命周期中防止self提早释放。strongSelf是一个自动变量当block执行完毕就会释放自动变量strongSelf不会对self进行一直进行强引用。

- (void)viewDidLoad {

    [super viewDidLoad];

    

    MyOBJ *mm = [[MyOBJ alloc]init];

    

    mm.name = @"Hanmeimei";

    __weak typeof(mm) weakSelf = mm;

    

    mm.doBlock = ^{

        __strong typeof(mm) strongSelf = weakSelf;

        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

            NSLog(@"my name is = %@",strongSelf.name);

        });

        

    };

    

    mm.doBlock();

}

//输出:my name is = Hanmeimei

 

weakSelf 是为了block不持有self,避免Retain Circle循环引用。
在 Block 内若是须要访问 self 的方法、变量,建议使用 weakSelf。

strongSelf的目的是由于一旦进入block执行,假设不容许self在这个执行过程当中释放,就须要加入strongSelf。block执行完后这个strongSelf 会自动释放,不会存在循环引用问题。
若是在 Block 内须要屡次 访问 self,则须要使用 strongSelf。

四、关于 多层嵌套的block
4.1单层block

- (void)doSomething{

    XXModel *model = [XXModel new];

    

    __weak typeof(self) weakSelf = self;

    

    model.dodoBlock = ^(NSString *title) {

        

        __strong typeof(self) strongSelf = weakSelf;

        strongSelf.titleLabel.text = title;

    };

    

    self.model = model;

}

 

- (void)viewDidLoad {

    [super viewDidLoad];

    [self doSomething];

}


4.2双层block

- (void)setUpModel{

    XXModel *model = [XXModel new];

    

    __weak typeof(self) weakSelf = self;

    

    model.dodoBlock = ^(NSString *title) {

        

        __strong typeof(self) strongSelf = weakSelf;//第一层

        strongSelf.titleLabel.text = title;

        

        __weak typeof(self) weakSelf2 = strongSelf;

        strongSelf.model.dodoBlock = ^(NSString *title2) {

            

            __strong typeof(self) strongSelf2 = weakSelf2;//第二层

            strongSelf2.titleLabel.text = title2;

        };

    };

    

    self.model = model;

}


这样就避免的引用循环,无论都多少个block嵌套,均可以按照这样来作。

 

 

 

转自:https://blog.csdn.net/nathan1987_/article/details/82749057

相关文章
相关标签/搜索