简体   繁体   English

为什么这里有保留周期?

[英]Why does it have retain cycle here?

Here is the code from Ray tutorial about ReactiveCocoa and I can't figure out how come it has retain cycle, can someone indicate that?这是 Ray 教程中关于ReactiveCocoa的代码,我不知道它为什么有保留周期,有人可以指出吗?

- (RACSignal *)signalForSearchWithText:(NSString *)text {

    // 1 - define the errors
    NSError *noAccountsError = [NSError errorWithDomain:RWTwitterInstantDomain
                                                   code:RWTwitterInstantErrorNoTwitterAccounts
                                               userInfo:nil];

    NSError *invalidResponseError = [NSError errorWithDomain:RWTwitterInstantDomain
                                                        code:RWTwitterInstantErrorInvalidResponse
                                                    userInfo:nil];

    // 2 - create the signal block
    @weakify(self)
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        @strongify(self);
        // 3 - create the request
        SLRequest *request = [self requestforTwitterSearchWithText:text];

        // 4 - supply a twitter account
        NSArray *twitterAccounts = [self.accountStore
                                    accountsWithAccountType:self.twitterAccountType];
        if (twitterAccounts.count == 0) {
            [subscriber sendError:noAccountsError];
        } else {
            [request setAccount:[twitterAccounts lastObject]];

            // 5 - perform the request
            [request performRequestWithHandler: ^(NSData *responseData,
                                                  NSHTTPURLResponse *urlResponse, NSError *error) {
                if (urlResponse.statusCode == 200) {

                    // 6 - on success, parse the response
                    NSDictionary *timelineData =
                    [NSJSONSerialization JSONObjectWithData:responseData
                                                    options:NSJSONReadingAllowFragments
                                                      error:nil];
                    [subscriber sendNext:timelineData];
                    [subscriber sendCompleted];
                }
                else {
                    // 7 - send an error on failure
                    [subscriber sendError:invalidResponseError];
                }
            }];
        }

        return nil;
    }];
}

Because I am trying to remove @weakify(self) and @stringify(self)因为我想删除@weakify(self)@stringify(self)

There isn't an obvious retain cycle in this code.这段代码中没有明显的保留循环。 You ought to be able to remove the @weakify/@strongify usages without a problem.您应该能够@weakify/@strongify地删除@weakify/@strongify用法。

In block , u shouldn't use self directly.在 block 中,你不应该直接使用 self 。 self keeps a block instance and the block keeps self. self 保持一个块实例,块保持自身。 U can use a weak self in block to avoid the retain cycle.你可以在块中使用一个弱的 self 来避免保留循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM