簡體   English   中英

在這種情況下,如何解決警告“在此塊中強烈捕獲‘自我’可能會導致保留周期”?

[英]How to resolve warning "Capturing 'self' strongly in this block is likely to lead to a retain cycle" in this case?

我的 PhotosListCollectionViewController.h 文件:

@interface PhotosListCollectionViewController : UICollectionViewController <UICollectionViewDelegateFlowLayout> {
    FooterView *footerView;
    PhotosListCollectionViewViewModel *photosListCollectionViewViewModel;
}

@property (strong, nonatomic) NSString *userQuery;

@end

在 PhotosListCollectionViewController.m 中(看評論):

@implementation PhotosListCollectionViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    footerView = [[FooterView alloc] init];

    photosListCollectionViewViewModel = [[PhotosListCollectionViewViewModel alloc] initWithUserQuery:_userQuery];

    [self.collectionView registerClass:FooterView.class forSupplementaryViewOfKind:UICollectionElementKindSectionFooter                withReuseIdentifier:NSStringFromClass([FooterView class])];

    __weak __typeof(self)weakSelf = self;

    photosListCollectionViewViewModel.getNextPage = ^(NSError *error) {
        if (nil == error) {
            [weakSelf.collectionView reloadData];
        } else {
            [weakSelf showAlertWithTitle:error.localizedDescription message:@"Try again."];
        }
        [self->footerView hideLoader]; // warning in this line. 
    };
}

如何解決我的問題? 我閱讀了另一個問題,但它們沒有解決我的問題。

改成

 [weakSelf->footerView hideLoader];

得到錯誤

由於競爭條件可能導致空值,不允許取消引用 __weak 指針,首先將其分配給強變量

感謝@matt幫助我。 決定是:

__weak __typeof(self)weakSelf = self;

_photosListCollectionViewViewModel.getNextPage = ^(NSError *error) {
    if (nil == error) {
        [weakSelf.collectionView reloadData];
    } else {
        [weakSelf showAlertWithTitle:error.localizedDescription message:@"Повторите попытку."];
    }
    __typeof(self)strongSelf = weakSelf;
    [strongSelf->footerView hideLoader];
};

暫無
暫無

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

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