ReactiveCocoa 对weakify和strongify 使用上的疑惑

ReactiveCocoa (RAC) 中 使用@weakify@strongify来避免循环引用
请各位帮忙看下我这么写有没有错误?

testControler

@interface testControler () 
@property(nonatomic, strong) DynamicListViewModel *dynamicListViewModel;
@end

@implementation testControler
- (void)initData {
    self.dynamicListViewModel = [[DynamicListViewModel alloc] init];
    @weakify(self);
    // 监听动态列表
    [RACObserve(self.dynamicListViewModel, dataArray) subscribeNext:^(id x) {
        // 这里用不用写 @strongify(self)?????
        [self.tbl_dymicList reloadData];
    }];

    [self.dynamicListViewModel.loadCommand execute:nil];

    [self.dynamicListViewModel.loadCommand.errors subscribeNext:^(NSError *x) {
        @strongify(self);
        [self showErrorMsg:x.domain];
    }];

    [[self.dynamicListViewModel.loadCommand.executing skip:1] subscribeNext:^(NSNumber *x) {
     // 这里用不用写 @strongify(self)?????
        if ([x boolValue]) {
            [self showProgress];
        } else {
            [self hideProgress];
        }
    }];

    [self.dynamicListViewModel.loadNextCommand.errors subscribeNext:^(NSError *x) {
        @strongify(self);
        [self showErrorMsg:x.domain];
    }];

}
@end

请各位讲解下注释中的疑问,能画下内存图更感激

阅读 2.7k
1 个回答

通过测试打印dealloc 方法确认代码中的疑问部分都需要使用@strongify(self)来打破循环引用

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进