Assignment to ivar in a Block via weak pointer
Asked Answered
P

2

8

I have a read-only property isFinished in my interface file:

typedef void (^MyFinishedBlock)(BOOL success, NSError *e);

@interface TMSyncBase : NSObject {
     BOOL isFinished_;
}

@property (nonatomic, readonly) BOOL isFinished;

and I want to set it to YES in a block at some point later, without creating a retain cycle to self:

- (void)doSomethingWithFinishedBlock:(MyFinishedBlock)theFinishedBlock {
    __weak MyClass *weakSelf = self;
    MyFinishedBlock finishedBlockWrapper = ^(BOOL success, NSError *e) {
        [weakSelf willChangeValueForKey:@"isFinished"];
        weakSelf -> isFinished_ = YES;
        [weakSelf didChangeValueForKey:@"isFinished"];
        theFinishedBlock(success, e);
    };

    self.finishedBlock = finishedBlockWrapper; // finishedBlock is a class ext. property
}

I'm unsure that this is the right way to do it. Will this code leak, or break, or is it fine? Perhaps there is an easier way I have overlooked?

Prodrome answered 4/7, 2012 at 10:23 Comment(2)
just fyi, you can use __weak typeof(self) *weakSelf = self;Wun
Small correction to the general declaration __weak typeof(self) weakSelf = self; typeof(self) is already a pointer.Forme
E
6

Passing block variable can be nil, check before calling or add assert on start of the function or you will crash

Since you are not retaining self and we assume that you execute some long task on background thread by the time your code get's executed weakSelf can be nil ( hopefully you are using ARC and 5.0 so you have niled weak references ).

If you don't have real weak references ( < 5.0, no ARC, compiler would still accept __weak but it wouldn't matter ) this would lead to crash.

Also accessing ivar using '->' will lead to crash if object pointer is nil so you need to make sure it doesn't happen.

Even if you do the code as dasblinkenlight wrote it can crash if weakSelf will be nil at the moment, let's say you dispatch the block on background thread and then object get released before block's executes, this makes weakSelf nil thus accesing it by using '->' will lead to crash. In that case I would modify the code as follows:

__weak MyClass *weakSelf = self;
MyFinishedBlock finishedBlockWrapper = ^(BOOL success, NSError *e) {
    MyClass *strongSelf = weakSelf;
    //! whatever task you want executed
    strongSelf.isFinished = YES;
    theFinishedBlock(success, e);
};

Also you could test if weakSelf is nil to prevent expensive task from execution if it doesn't make sense ( object is already destroyed ). But this depends on use case.

But there is also other case you need to consider when programming with blocks, for example: You can have a job object instance that's only role is to execute some task in background, in that case this code could fail because you would create new task and it could be deallocated before block executes on background thread, in that case you should retain self and don't retain block in the object ( this will prevent retain cycle ).

Extern answered 4/7, 2012 at 10:40 Comment(4)
isFinished is a readonly property, so I don't want strongSelf.isFinished = YES to work.. it's there just for KVOProdrome
Specify is a normal assign in private category, this will make it make isFinished writable in your class code but readonly outside of it, this will also skip unnecessary manual KVO notifications.Hydrothorax
so why is strongSelf necessary?Mangle
strongSelf will retain the object for the time the block body is executed if on entry the object still existed. That way object won't release in the middle of your block code.Hydrothorax
B
1

A slight workaround is to create a method and let that the compiler handle it for you. Works fine, but I'm not sure if it is the correct way. Can someone tell if its correct?

__weak MyClass *weakSelf = self;
MyFinishedBlock finishedBlockWrapper = ^(BOOL success, NSError *e) {
    [weakSelf makeIsFinishedYes];
};

- (void)makeIsFinishedYes
{
    isFinished_ = YES;
}
Backlog answered 7/5, 2013 at 10:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.