Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing an instance variable in a block

I am quite confused about how to change an instance variable inside of a block.

The interface file (.h):

@interface TPFavoritesViewController : UIViewController {
    bool refreshing;
}

The implementation:

__weak TPFavoritesViewController *temp_self = self;
refreshing = NO;
[myTableView addPullToRefreshWithActionHandler:^{
    refreshing = YES;
    [temp_self refresh];
}];

As you might guess, I get a retain cycle warning when I try to change the refreshing ivar inside of the block. How would I do this without getting an error?

like image 517
Keiran Paster Avatar asked Aug 01 '12 04:08

Keiran Paster


1 Answers

Your assignment to refreshing is an implicit reference to self, it is shorthand for:

self->refreshing = YES;

hence the cycle warning. Change it to:

temp_self->refreshing = YES;
like image 151
CRD Avatar answered Sep 30 '22 07:09

CRD