I have a situation like this:
- (void)someMethod
{
__weak typeof(self) weakSelf = self;
void (^myBlock1)(void) = ^{
// ... do stuff
};
void (^myBlock2)(void) = ^{
[weakSelf doSomeHeavyNetworkCall2:^{
myBlock1();
}];
};
[self doSomeHeavyNetworkCall1:^{
myBlock2();
}];
}
What is the life cycle of myBlock1 and myBlock2 in this case? Should I be checking for nil like this?
void (^myBlock2)(void) = ^{
[weakSelf doSomeHeavyNetworkCall2:^{
if (myBlock1) {
myBlock1();
}
}];
};
[self doSomeHeavyNetworkCall1:^{
if (myBlock2) {
myBlock2();
}
}];
Also if I wrap the entire someMethod
in @synchronized
, am I guaranteed to have the blocks around?
Local variables are strong by default in Objective-C so myBlock1
and myBlock2
are strong. There is no need for the if (myBlock1)
and if (myBlock2)
checks.
Wrapping the contents of someMethod
with @synchronized
has no effect on the lifecycle of these variables.