OGeek|极客世界-中国程序员成长平台

标题: ios - block 不捕获 self [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 18:02
标题: ios - block 不捕获 self

我想尝试了解 block 捕获逻辑,但现在我对此有疑问。我有 MeRequestNSNumber 属性。

@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)meInfoSuccessBlockRequestSuccessBlock)success failureBlockRequestFailureBlock)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 *)performWithCompletionvoid(^)(id responseObject))completion
                                        onFailurevoid(^)(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;
}

并且在 failureBlock 我将数字设置为属性。当我离开此 Controller 时,我在控制台中看到了 dealloc 消息,该 Controller 已被释放。

- (void)dealloc {
    NSLog(@"%s", __PRETTY_FUNCTION__);
}

为什么 Controller 会解除分配?我不使用对 self

的弱引用



Best Answer-推荐答案


要明确知道,您必须发布 MeRequest 类的实现。

在不知不觉中,这是一个有根据的猜测。

通过 meInfoSuccessBlock:failureBlock: 传递给 self.request 的 block 在事务完成时可能会被清空。也就是说,它可能是这样的:

- (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 来打破循环。


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

- (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
}

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

关于ios - block 不捕获 self ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34555049/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4