简体   繁体   English

我需要引用AFNetworkingReachability块中的弱项吗?

[英]Would I need to refer to self with weak in AFNetworkingReachability block

As the title says: Do I need to make a weak reference to self, when calling functions in a AFNetworkingReachability block like so? 就像标题所说的那样,当在AFNetworkingReachability块中调用函数时,我是否需要对self进行弱引用?

__weak myViewController* weakSelf = self;

     [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status)
        {

            switch (status) {
            case AFNetworkReachabilityStatusReachableViaWiFi:

                DLog(@"No Internet Connection");

                NSLog([weakSelf reachable] ? @"YES" : @"NO");

               weakSelf.aProperty = aPropertyValue. 

               [weakSelf loadPlaces]; // This function does also contain a block

             break;
            default:

                break;
            }
        }];

Thank you in advance. 先感谢您。

If your view controller doesn't have a strong reference (either directly or indirectly) to AFNetworkReachabilityManager sharedManager , then no. 如果您的视图控制器没有(直接或间接)对AFNetworkReachabilityManager sharedManager的强引用, AFNetworkReachabilityManager sharedManager否。 You only need to follow this pattern to break strong reference cycles. 您只需要遵循这种模式即可打破强大的参考周期。

Watch our for indirect strong reference cycles, which clang won't warn you about. 观看我们的间接强参考周期,叮当声不会警告您。 For example, your view controller could have a strong property to an API object, which could have a reference to AFHTTPRequestOperationManager, which could strongly reference the reachability manager. 例如,您的视图控制器可能具有API对象的strong属性,该对象可以引用AFHTTPRequestOperationManager,而AFHTTPRequestOperationManager可以强烈引用可达性管理器。

If you're following the correct rules for properties - only using strong to indicate ownership, and weak otherwise - you will usually be okay. 如果您遵循正确的属性规则-仅使用strong表示所有权,否则使用weak -通常就可以了。

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

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