美文网首页iOS归纳
iOS - Block 循环引用

iOS - Block 循环引用

作者: 码代码的小马 | 来源:发表于2021-05-17 14:39 被阅读0次

    我们知道,循环引用即:
    当两个对象A和B, 分别强引用对方,那么就会产生循环引用。即A释放的时候必须先释放B,而B释放的时候必须释放A。导致谁也不能释放
    而打破循环引用的方法就是其中一方弱引用另一方

    1. Block是否一定会造成循环引用

    根据上篇文我们知道,循环引用的关键是:相互强引用,如果没有达到这一必要条件,则block不会造成循环引用

    两个简单的例子:

    @interface BlockViewController ()
    
    @property (nonatomic, copy) NSString *nickname;
    
    @property (nonatomic, copy) void(^ testBlock1)(void);
    
    @property (nonatomic, copy) void(^ testBlock2)(void);
    
    @end
    
    @implementation BlockViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        self.view.backgroundColor = [UIColor orangeColor];
        
        // 会造成循环引用,因为self->Block, block -> self
        self.testBlock1 = ^{
            NSLog(@"== self = %p",self);
        };
    
        //不会造成循环引用,因为只有self->block
        self.testBlock2 = ^{
        };
    }
    
    - (void)dealloc {
        NSLog(@"dealloc = %@", self);
    }
    
    @end
    
    

    编译成cpp源码:

    testBlock0

    struct __BlockViewController__viewDidLoad_block_impl_0 {
      struct __block_impl impl;
      struct __BlockViewController__viewDidLoad_block_desc_0* Desc;
      BlockViewController *self;
      __BlockViewController__viewDidLoad_block_impl_0(void *fp, struct __BlockViewController__viewDidLoad_block_desc_0 *desc, BlockViewController *_self, int flags=0) : self(_self) {
        impl.isa = &_NSConcreteStackBlock;
        impl.Flags = flags;
        impl.FuncPtr = fp;
        Desc = desc;
      }
    };
    static void __BlockViewController__viewDidLoad_block_func_0(struct __BlockViewController__viewDidLoad_block_impl_0 *__cself) {
      BlockViewController *self = __cself->self; // bound by copy
    
            NSLog((NSString *)&__NSConstantStringImpl__var_folders_44_1ht3l6g55dv59_5s62wsv_bm0000gn_T_BlockViewController_439f5f_mi_0,self);
        }
        
            ((void (*)(id, SEL, void (*)()))(void *)objc_msgSend)((id)self, sel_registerName("setTestBlock1:"), ((void (*)())&__BlockViewController__viewDidLoad_block_impl_0((void *)__BlockViewController__viewDidLoad_block_func_0, &__BlockViewController__viewDidLoad_block_desc_0_DATA, self, 570425344)));
    

    testBlock1:

    struct __BlockViewController__viewDidLoad_block_impl_1 {
      struct __block_impl impl;
      struct __BlockViewController__viewDidLoad_block_desc_1* Desc;
      __BlockViewController__viewDidLoad_block_impl_1(void *fp, struct __BlockViewController__viewDidLoad_block_desc_1 *desc, int flags=0) {
        impl.isa = &_NSConcreteStackBlock;
        impl.Flags = flags;
        impl.FuncPtr = fp;
        Desc = desc;
      }
    };
    static void __BlockViewController__viewDidLoad_block_func_1(struct __BlockViewController__viewDidLoad_block_impl_1 *__cself) {
    
        }
    
        ((void (*)(id, SEL, void (*)()))(void *)objc_msgSend)((id)self, sel_registerName("setTestBlock2:"), ((void (*)())&__BlockViewController__viewDidLoad_block_impl_1((void *)__BlockViewController__viewDidLoad_block_func_1, &__BlockViewController__viewDidLoad_block_desc_1_DATA)));
    

    或者如下:也不会造成循环引用,虽然block持有self,但self没有持有block

        BlockViewController2 *vc = [[BlockViewController2 alloc] init];
        [self.navigationController pushViewController:vc animated:true];
        vc.testBlock10 = ^{
            NSLog(@"-----vc.testBlock10 = %p", self);
        };
    

    2. UIView动画是否会造成循环引用

    不会,+ (void)animateWithDuration:(NSTimeInterval)duration animations:是类方法,当前控制器无法强引用一个类,所以循环引用无法构成

    3. Masonry是否会造成循环引用

    不会 , self持有了Masonry,但是Masonry源码中并没有持有View,,如果将源码的block(constrainMaker)改成self.block = block(constrainMaker),那么此时view才会持有block,才会造成循环引用

        self.testButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.view addSubview:self.testButton];
        [self.view mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.view);
            make.centerY.equalTo(self.view);
            make.size.mas_equalTo(CGSizeMake(100, 50));
        }];
     
     Masonry源码:
    - (NSArray *)mas_makeConstraints:(void(^)(MASConstraintMaker *))block {
        self.translatesAutoresizingMaskIntoConstraints = NO;
        MASConstraintMaker *constraintMaker = [[MASConstraintMaker alloc] initWithView:self];
        block(constraintMaker);
        return [constraintMaker install];
    }
    

    4. AFN 是否会造成循环引用

    项目中用到AFN的代码

        AFHTTPSessionManager * manager = [AFHTTPSessionManager manager];
        [manager POST:completeURL parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            if (success) {
                success(responseObject);
            }
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            if (failure) {
                failure(error);
            }
        }];
    

    AFN源码

    - (NSURLSessionDataTask *)POST:(NSString *)URLString
                        parameters:(id)parameters
                          progress:(void (^)(NSProgress * _Nonnull))uploadProgress
                           success:(void (^)(NSURLSessionDataTask * _Nonnull, id _Nullable))success
                           failure:(void (^)(NSURLSessionDataTask * _Nullable, NSError * _Nonnull))failure
    {
        NSURLSessionDataTask *dataTask = [self dataTaskWithHTTPMethod:@"POST" URLString:URLString parameters:parameters uploadProgress:uploadProgress downloadProgress:nil success:success failure:failure];
    
        [dataTask resume];
    
        return dataTask;
    }
    
    - (NSURLSessionDataTask *)dataTaskWithHTTPMethod:(NSString *)method
                                           URLString:(NSString *)URLString
                                          parameters:(id)parameters
                                      uploadProgress:(nullable void (^)(NSProgress *uploadProgress)) uploadProgress
                                    downloadProgress:(nullable void (^)(NSProgress *downloadProgress)) downloadProgress
                                             success:(void (^)(NSURLSessionDataTask *, id))success
                                             failure:(void (^)(NSURLSessionDataTask *, NSError *))failure
    {
        NSError *serializationError = nil;
        NSMutableURLRequest *request = [self.requestSerializer requestWithMethod:method URLString:[[NSURL URLWithString:URLString relativeToURL:self.baseURL] absoluteString] parameters:parameters error:&serializationError];
        if (serializationError) {
            if (failure) {
                dispatch_async(self.completionQueue ?: dispatch_get_main_queue(), ^{
                    failure(nil, serializationError);
                });
            }
    
            return nil;
        }
    
        __block NSURLSessionDataTask *dataTask = nil;
        dataTask = [self dataTaskWithRequest:request
                              uploadProgress:uploadProgress
                            downloadProgress:downloadProgress
                           completionHandler:^(NSURLResponse * __unused response, id responseObject, NSError *error) {
            if (error) {
                if (failure) {
                    failure(dataTask, error);
                }
            } else {
                if (success) {
                    success(dataTask, responseObject);
                }
            }
        }];
    
        return dataTask;
    }
    

    可见: self并没有持有manager, dataTask也没有持有success和failure这两个block,即使block中持有了self。也不会构成循环引用

    相关文章

      网友评论

        本文标题:iOS - Block 循环引用

        本文链接:https://www.haomeiwen.com/subject/hbrjjltx.html