简体   繁体   English

“使用可达性”“在此区块中强烈捕获'自我'很可能导致保留周期”

[英]“Capturing 'self' strongly in this block is likely to lead to a retain cycle” using Reachability

I'm trying to edit a variable inside the Reachability block using Objective-C, this is the code: 我正在尝试使用Objective-C编辑Reachability块内的变量,这是代码:

- (void)testInternetConnection
{
    internetReachableFoo = [Reachability reachabilityWithHostname:@"www.google.com"];
    // Internet is reachable
    internetReachableFoo.reachableBlock = ^(Reachability*reach)
    {
        // Update the UI on the main thread
        dispatch_async(dispatch_get_main_queue(), ^{
            NSLog(@"Connessione ad Internet disponibile");
            checkConnection = YES;
            if(!lastConnectionState)
            {
                lastConnectionState = YES;
                if(doItemsDownload)
                    [self displayChoice];
            }
        });
    };

    // Internet is not reachable
    internetReachableFoo.unreachableBlock = ^(Reachability*reach)
    {
        // Update the UI on the main thread
        dispatch_async(dispatch_get_main_queue(), ^{
            NSLog(@"Connessione ad Internet non disponibile");
            checkConnection = NO;
            lastConnectionState = NO;
        });
    };

    [internetReachableFoo startNotifier];
}

Where checkConnection; 哪里checkConnection; & lastConnectionState; lastConnectionState; are 2 bool declared on my @interface; 在我的@interface上声明了2个布尔值; The problem is that accessing these variables and calling [self displayChoice]; 问题在于访问这些变量并调用[self displayChoice]; inside this block gives me the warning: Capturing 'self' strongly in this block is likely to lead to a retain cycle 在此块内向我发出警告: Capturing 'self' strongly in this block is likely to lead to a retain cycle

How can I possibly avoid this error? 如何避免该错误? I tried declaring a WeakSelf and declaring self but I don't know how to do it for the bool variables 我试图声明一个WeakSelf并声明self但我不知道如何为布尔变量做它

Capturing self strongly in a block is not always bad. 在块中强烈地捕捉自我并不总是坏事。 If a block is being executed right away (UIView animate block for example) there is generally no risk. 如果立即执行块(例如,UIView动画块),通常就没有风险。

The problem arises when self captures a block strongly and the block in turn captures self strongly. 当自我强烈捕获自己的块,而反过来又强烈捕获自己的块时,就会出现问题。 In this case self retains the block and the block retains self so neither can be released --> retain cycle! 在这种情况下,self将保留块,而block将保留自身,因此无法释放->保留循环!

To avoid this you need to capture self weakly in the block. 为了避免这种情况,您需要在区块中弱势地捕捉自我。

__weak typeof(self) = self;  // CREATE A WEAK REFERENCE OF SELF
__block BOOL blockDoItemsDownload = doItemsDownload;  // USE THIS INSTEAD OF REFERENCING ENVIRONMENT VARIABLE DIRECTLY
__block BOOL blockCheckConnection = checkConnection;
internetReachableFoo.reachableBlock = ^(Reachability*reach)
{
    // Update the UI on the main thread
    dispatch_async(dispatch_get_main_queue(), ^{
        NSLog(@"Connessione ad Internet disponibile");
        blockCheckConnection = YES;
        if(!lastConnectionState)
        {
            lastConnectionState = YES;
            if(blockDoItemsDownload)       // Use block variable here
                [weakSelf displayChoice];  // Use weakSelf in place of self
        }
    });
};

There is a cocoapod called libextobjc and what it allows you to do is quickly and cleanly weakify and strongify objects. 有一个名为libextobjc的cocoapod,它可以让您快速干净地削弱和增强对象。

@weakify(self)
[someblock:^{
    @strongify(self)
}];

As long as you handle self you should be ok. 只要你能自我处理就可以了。 I'm not 100% sure the BOOL values aren't an issue but I think you could do something like this: 我不确定100%的BOOL值不是问题,但我认为您可以执行以下操作:

BOOL x = YES;
@weakify(self, x)
[someblock:^{
    @strongify(self, x)
}];

暂无
暂无

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

相关问题 避免“在此块中强烈捕获自我可能会导致保留周期”消息 - Avoiding the “capturing self strongly in this block is likely to lead to a retain cycle” message 在这个区块中强烈捕获自我可能会导致保留周期 - capturing self strongly in this block is likely to lead to a retain cycle 如何在此块中强烈修复“捕获'块'可能会导致保留周期” - How to fix “Capturing 'block' strongly in this block is likely to lead to a retain cycle” iOS 5 Twitter Framework和completionHandler块 - “在这个块中强烈捕获'自我'可能会导致保留周期” - iOS 5 Twitter Framework & completionHandler block - “Capturing 'self' strongly in this block is likely to lead to a retain cycle” 在这种情况下,如何解决警告“在此块中强烈捕获‘自我’可能会导致保留周期”? - How to resolve warning "Capturing 'self' strongly in this block is likely to lead to a retain cycle" in this case? 警告:在此块中强烈捕获$ XXX可能会导致“ AFHTTPClient.m”中的保留周期 - Warning: Capturing $XXX strongly in this block is likely to lead to a retain cycle in 'AFHTTPClient.m' 块中的@synchronized(self)是否会导致保留周期? - Does @synchronized(self) in a block lead to a retain cycle? 强烈阻止捕捉“自我” - Block capturing 'self' strongly 捕获“自身”时,弱保留的块如何导致保留周期 - How can a weakly retained block cause a retain cycle when capturing “self” 缺少“可能导致保留周期”警告是否意味着将不会创建强参考周期? - Does the absence of a “likely to lead to a retain cycle” warning imply a strong reference cycle will not be created?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM