I am trying to call a method on super class inside a block. In order to avoid a retain-cycle I need a weak reference to super. How would I get a weak reference to super?
[self somethingWithCompletion:^(){
[super doStuff];
}];
I tried the following but gives a compile error.
__weak MySuperClass *superReference = super;
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.
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.
The simplest way to create such scope is to declare another block: void(^intermediateBlock)(__typeof(self) __weak self) = ^(__typeof(self) __weak self) { // self is weak in this scope ^{ // self is weak here too [self doSomeWork]; }; };
Super is self , but when used in a message expression, it means "look for an implementation starting with the superclass's method table."
You could define a helper method
-(void) helperMethod
{
[super doStuff];
// ...
[super doOtherStuff];
// ...
}
and then do
__weak MyClass *weakSelf = self;
[self somethingWithCompletion:^(){
MyClass *strongSelf = weakSelf;
[strongSelf helperMethod];
}];
A direct solution using the runtime methods looks like this:
__weak MyClass *weakSelf = self;
[self somethingWithCompletion:^(){
MyClass *strongSelf = weakSelf;
if (strongSelf) {
struct objc_super super_data = { strongSelf, [MyClass superclass] };
objc_msgSendSuper(&super_data, @selector(doStuff));
}
});
Disadvantages (in my opinion):
objc_msgSendSuper
or objc_msgSendSuper_stret
.objc_msgSendSuper
to the proper
function type (thanks to @newacct).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