I have a question about strong and weak references to self in blocks in iOS. I know the proper way to refer to self inside a block is to create a weak reference outside the block, and then a strong reference to that weak reference inside the block, like this:
__weak typeof(self) weakSelf = self; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^ { typeof(self) strongSelf = weakSelf; NSLog(@"%@", strongSelf.someProperty); });
However, what happens if you have nested blocks? Is the one set of references enough? Or do you need a new set for each block? For example, which of the following is correct?
This:
__weak typeof(self) weakSelf = self; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^ { typeof(self) strongSelf = weakSelf; NSLog(@"%@", strongSelf.someProperty); dispatch_async(dispatch_get_main_queue(), ^ { strongSelf.view.frame = CGRectZero; }); });
Or this:
__weak typeof(self) weakSelf = self; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^ { typeof(self) strongSelf = weakSelf; NSLog(@"%@", strongSelf.someProperty); __weak typeof(strongSelf) weakSelf1 = strongSelf; dispatch_async(dispatch_get_main_queue(), ^ { typeof(strongSelf) strongSelf1 = weakSelf1; strongSelf1.view.frame = CGRectZero; }); });
Any information or explanation is much appreciated!
strong is the default. An object remains “alive” as long as there is a strong pointer to it. weak specifies a reference that does not keep the referenced object alive. A weak reference is set to nil when there are no strong references to the object.
Strong References. The opposite of a strong reference is a weak reference. In Swift, strong references are the default, so to make a reference weak you can use the weak keyword. Unlike strong references, a weak reference does not affect an instance's retain count.
A reference to an object is any object pointer or property that lets you reach the object. There are two types of object reference: Strong references, which keep an object “alive” in memory. Weak references, which have no effect on the lifetime of a referenced object.
Pointers that are not retained are often referred to as “weak” in Objective-C documentation that predates the garbage collector. These are references that are allowed to persist beyond the lifetime of the object. Unfortunately, there is no automatic way of telling whether they are still valid.
You don’t need to make two sets of weak references. What you want to avoid with blocks is a retain cycle—two objects keeping each other alive unnecessarily.
If I have an object with this property:
@property (strong) void(^completionBlock)(void);
and I have this method:
- (void)doSomething { self.completionBlock = ^{ [self cleanUp]; }; [self doLongRunningTask]; }
the block will be kept alive when I store it in the completionBlock
property. But since it references self
inside the block, the block will keep self
alive until it goes away—but this won’t happen since they’re both referencing each other.
In this method:
- (void)doSomething { [[NSOperationQueue mainQueue] addOperationWithBlock:^{ [self cleanUp]; }]; [self doLongRunningTask]; }
you don’t need to make a weak reference to self
. The block will keep self
alive, since it references self
from within, but since all we’re doing is handing the block off to [NSOperationQueue mainQueue]
, self
isn’t keeping the block alive.
Hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With