objective c - blocks and async callback, dealloc object - need to nil the block -
there similar question here, doesn't explain want: objective c blocks async-callbacks & bad access
i have view controller, calls service async callback. callback done using block, references variables on view controller populate them.
it looks so:
- (void) loaddata { __block myviewcontroller *me = self; [self.service executewithcompletion:^(nsarray *result, nserror *error) { if (!error) { me.data = result; } }]; } however, if dealloc view controller, 'me' badly accessed callback.
what simplest way of making 'me' null? if put ivar, brings circular reference... think?
i think i'm missing obvious....
thanks
are targeting ios 5.0 or later (or mac os x 10.7 or later)? if so, can use arc , __weak variable (instead of __block one). automatically 0 out when referenced object deallocated. code like
- (void)loaddata { __weak myviewcontroller *me = self; [self.service executewithcompletion:^(nsarray *result, nserror *error) { if (!error) { myviewcontroller *strongme = me; // load __weak var strong if (strongme) { strongme.data = result; } } }]; } if need support older os need find different solution. 1 solution go ahead , let block retain self. if service guaranteed execute completion block (and release it), produce temporary cycle break automatically when completion block run. alternatively if have way cancel service (in way guarantees block cannot called after cancellation), can stick __block , sure cancel service in -dealloc. there's other alternatives they're more complicated.
Comments
Post a Comment