Block不会捕获自我

时间:2016-01-01 09:41:01

标签: ios objective-c memory-management

我想尝试理解块捕获逻辑,现在我对它有疑问。我有eth0MeRequest属性。

NSNumber

然后,在@property (nonatomic) MeRequest *request; @property (nonatomic) NSNumber *number; 我呼叫请求方法

viewDidLoad

self.request = [[MeRequest alloc] init]; [self.request meInfoSuccessBlock:^(NSDictionary *response) { } failureBlock:^(Error *error) { self.number = @5; }]; - (void)meInfoSuccessBlock:(RequestSuccessBlock)success failureBlock:(RequestFailureBlock)failure { self.method = @"GET"; self.parameters = @{}; [self performWithCompletion:^(id responseObject) { NSDictionary *response = (NSDictionary *)responseObject; if (success) { success(response); } } onFailure:^(Error *error) { if (failure) { failure(error); } }]; } - (AFHTTPRequestOperation *)performWithCompletion:(void(^)(id responseObject))completion onFailure:(void(^)(Error *error))failure { NSURLRequest *request = [[NetworkManager sharedManager] requestWithMethod:self.method path:self.path parameters:self.parameters]; if (_operation) { [_operation cancel]; } _operation = [[NetworkManager sharedManager] HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) { _operation = nil; dispatch_semaphore_signal(_semaphore); if (completion) { completion(responseObject); } } failure:^(AFHTTPRequestOperation *operation, NSError *error) { _operation = nil; dispatch_semaphore_signal(_semaphore); if (failure) { failure(_error); } }]; [_operation start]; return _operation; } 中,我将号码设置为属性。当我离开这个控制器时,我在控制台中看到了dealloc消息,该控制器已被解除分配。

failureBlock

为什么控制器deallocs?我不使用弱引用- (void)dealloc { NSLog(@"%s", __PRETTY_FUNCTION__); }

1 个答案:

答案 0 :(得分:2)

要明确了解,您必须发布MeRequest类的实现。

不知道这一点,这是一个有根据的猜测。

当交易完成时,通过self.request传递到meInfoSuccessBlock:failureBlock:的块可能会被取消。也就是说,它可能是这样的:

- (void)meInfoSuccessBlock:... sB failureBlock:... fB {
   _sB = sB; // put ref in ivar
   _fB = fB; // because this is probably broken up across methods
   dispatch_async(_queue, ^{
        .... think hard ...
        if (success) _sB(...);
        else _fB(...);
        _sB = nil;
        _fB = nil;
   };
}

因此,首先,您不是创建直接循环引用,而是 - 可能 - self -> request -> _sB -> self的循环引用。其次,通过在完成计算并进行回调后分配_sB = nil,循环就会中断。

或者,在您的情况下,您对仅在范围内存在的块具有强引用。即有点像这样:

- (void)meInfoSuccessBlock:... sB failureBlock:... fB {
   dispatch_async(_queue, ^{
        .... think hard ...
        if (success) sB(...);
        else fB(...);
        // when the block finishes execution, fB and sB will be released
   };
   // when execution gets to here, the block above is the only strong references to sB and fB
}

也就是说,当你有一个保留周期时,该周期中的一个引用明确地与回调块的生命周期相关联,并且因为这些引用仅在回调完成之前存在,它们会被破坏并且会破坏循环。