Objective-C - Block and memory management?

__weak MyClass *selfReference = self;

dispatch_async(dispatch_get_main_queue(), ^{
        [selfReference performSomeAction];
    });
  • When do you need to pass a weak link to a block?
  • Does this rule apply to dispatch_async as well as user blocks?
  • Is the block a copy of the iVars used in it, or does it save them?
  • Who owns the variables initialized inside the block? Who should free them?
+5
source share
1 answer

1, 2) ( , dispatch_async ). , , , ( self) . , :

__weak MyClass *weakSelf = self;
self.block = ^{
    MyClass *strongSelf = weakSelf;
    ...
    [strongSelf ...];
    [strongSelf.property ...];
    [strongSelf->iVar ...];
 }

. iVar, self- > iVar , , self!

3) , .

4) , , , .

+10

All Articles