iOS - Block 循环引用

简介: 我们知道,循环引用即:当两个对象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开发
iOS block修饰符用copy还是strong
iOS block修饰符用copy还是strong
144 0
|
iOS开发 Python
iOS小技能:lldb打印block参数签名
iOS逆向时经常会遇到参数为block类型,本文介绍一个lldb script,可快速打印出Objective-C方法中block参数的类型。
199 0
iOS小技能:lldb打印block参数签名
|
iOS开发
iOS循环引用深入理解
循环引用也就是引用之后造成的一个循环用图理解吧
239 0
iOS循环引用深入理解
|
iOS开发 开发者
iOS开发 - 如何写出漂亮的block
iOS开发 - 如何写出漂亮的block
104 0
|
iOS开发
iOS开发- 关于Block的几种应用
iOS开发- 关于Block的几种应用
117 0
|
自然语言处理 iOS开发
IOS——Block
IOS——Block
79 0
|
iOS开发
iOS代理 通知 block传值的规范写法
iOS代理 通知 block传值的规范写法
145 0
|
存储 API iOS开发
iOS Principle:Block(下)
iOS Principle:Block(下)
131 0
iOS Principle:Block(下)
|
C语言 iOS开发 C++
iOS Principle:Block(上)
iOS Principle:Block(上)
115 0
iOS Principle:Block(上)
|
存储 中间件 Java
iOS-底层原理 30:Block底层原理
iOS-底层原理 30:Block底层原理
285 0
iOS-底层原理 30:Block底层原理