iOS - Block循環引用

上篇iOS - 循環引用markdown

咱們知道,循環引用即: 當兩個對象A和B, 分別強引用對方,那麼就會產生循環引用。即A釋放的時候必須先釋放B,而B釋放的時候必須釋放A。致使誰也不能釋放 而打破循環引用的方法就是其中一方弱引用另外一方async

#1. Block是否必定會形成循環引用 根據上篇文咱們知道,循環引用的關鍵是:相互強引用,若是沒有達到這一必要條件,則block不會形成循環引用post

兩個簡單的例子:動畫

@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源碼:atom

testBlock0spa

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:code

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沒有持有blockorm

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,纔會形成循環引用get

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。也不會構成循環引用

相關文章
相關標籤/搜索