簡體   English   中英

為什么這里有保留周期?

[英]Why does it have retain cycle here?

這是 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;
    }];
}

因為我想刪除@weakify(self)@stringify(self)

這段代碼中沒有明顯的保留循環。 您應該能夠@weakify/@strongify地刪除@weakify/@strongify用法。

在 block 中,你不應該直接使用 self 。 self 保持一個塊實例,塊保持自身。 你可以在塊中使用一個弱的 self 來避免保留循環。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM