I have some typedef:
typedef void (^myBlock)(SomeObject);
And I have some object
@interface SomeObject : NSObject
@end
// Method of some arbitrary class
- (void) someMethod1 {
SomeObject *someObject = [[SomeObject alloc] init];
myBlock block = ^(SomeObject obj){
// When _block(someObject)_ will be called inside someQueue -
// Is it guaranteed that someObject will be alive, retained inside me?
// Do something complex and involving (or not) obj ...
}
dispatch_async(someQueue, ^{
// Some bunch of code - after which we are sure that
// by the next line someMethod1 will run out, so its scope is lost
block(someObject);
});
}
The question is put inside the block variable’s block: is it guaranteed that someObject object we pass to the block block inside someQueue queue will be alive and retained inside the block block?
This question is a bit more complex variation of the question I’ve just asked: Does a passing of an object to a block guarantee that its lifecycle will be retained?.
I believe that what you’re asking is the same of the previous question.The only difference here is that you aren’t calling a method inside the block, but you are calling a block.
Maybe my answer wasn’t enough clear: everything is retained inside a block, even a block is retained.
Inside the block you call someObject, so someObject is captured.Also the block is captured.