-1

My PhotosListCollectionViewController.h file:

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

@property (strong, nonatomic) NSString *userQuery;

@end

In PhotosListCollectionViewController.m (look comment):

@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. 
    };
}

How to resolve my problem? I read another questions, but they did not solve my problem.

Change to

 [weakSelf->footerView hideLoader];

get error

Dereferencing a __weak pointer is not allowed due to possible null value caused by race condition, assign it to strong variable first

Tkas
  • 302
  • 3
  • 14
  • The point of creating `weakself` is so that you do not capture self. But then you mess it up when you _do_ capture self. You say `self->footerView`. Hence the warning. – matt Aug 18 '19 at 12:48
  • 1
    And besides you are not doing the dance correctly. See http://www.apeth.com/iOSBook/ch12.html#EXstrongWeakDance – matt Aug 18 '19 at 12:51
  • @matt yes, but change to `[weakSelf->footerView hideLoader];` not working. – Tkas Aug 18 '19 at 12:52
  • 1
    But you didn’t read my other comment or follow the link. You are not doing the weak strong dance correctly; look at minute 27:10 of Session 322 of the WWDC 2011 videos. – matt Aug 18 '19 at 13:08
  • @matt yes, decision is ` __typeof(self)self = weakSelf; [self->footerView hideLoader]; ` thx – Tkas Aug 18 '19 at 13:10

1 Answers1

0

Thanks @matt for help me. Decision is:

__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];
};
Tkas
  • 302
  • 3
  • 14