ARC implementation of own property upon transfer

I recently switched to ARC and got a little confused about setting the correct property value for a self-object, passing it to a block.

In my understanding, any property declared with _weak should not be set to zero in the dealloc method. And the objects themselves transferred to the block should be declared with _weak , and not __ block .

Please let me know if this understanding is correct, and I am doing it correctly in the implementation below.

(void)myApplication { self.data = [NSMutableDictionary dictionary]; __weak MyViewController *aBlockSelf = self; [self.data setValue:[MyAction customActionWithBlock:^(MyAction *iAction, NSString *iIdentifier) { AnotherViewController *aController = [[AnotherViewController alloc] initWithType:@"aType"]; aController.hasSearch = NO; aController.delegate = aBlockSelf; aController.showInventoryImage = YES; [aBlockSelf presentNavigationalModalViewController: aController]; }] forKey:@"aKey"]; } 
+4
source share
2 answers

In my understanding, any property declared with __weak should not be set to nil in the dealloc method.

Yes, there is absolutely no reason why you will ever want to do this. This is not a problem, but does not achieve anything.

And self- __weak passed to the block should be declared using __weak , not __block .

Yes, in ARC, using __weak mitigates the risk of cycle retention (aka strong reference cycle). This is important when saving a block for some variable, as in your example, or if the block works asynchronously. See Avoid strong reference cycles when capturing yourself in a Program using the Objective-C Guide.

Please let me know if this understanding is correct, and I am doing it correctly in the implementation below.

My only suggestion in your code block is that for this purpose you usually see a variable called weakSelf , not aBlockSelf . It doesn’t matter, but it makes the code more understandable.

+4
source

What you do is right.

 __weak MyViewController *aBlockSelf = self; 

In addition, when using ARC, you should not redefine dealloc unless you have created and maintained references to Core Foundation objects. When you redefine dealloc , do not call [super dealloc] .

0
source

Source: https://habr.com/ru/post/1483252/


All Articles