References to local variables (including ivars) inside blocks are automatically retained to make sure it remains valid throughout the execution of the block.
That said, would this code lead to a premature deallocation of self.message, since the first block does not reference it, even though the second block does? Or is it smart enough to know it should be retained?
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
/* snip */
dispatch_async(dispatch_get_main_queue(), ^{
if ([self.message.ID isEqualToString:@"foobar"]) {
/* snip */
}
});
});
A block captures all variables inside it, including nested blocks. In short: it does the “right” thing.
In this particular case, note that it’s
selfthat’s being captured, notself.message.