通过弱指针分配到块中的ivar

时间:2022-02-07 14:21:02

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

我的界面文件中有一个只读属性isFinished:

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:

我想稍后在一个块中将它设置为YES,而不创建一个自我保留周期:

- (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?

我不确定这是正确的做法。这段代码会泄漏,破坏,还是没问题?也许有一种我更容易被忽视的方式?

2 个解决方案

#1


5  

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

传递块变量可以是nil,在调用之前检查或在函数启动时添加断言,否则你将崩溃

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 ).

因为你没有保留自我,我们假设你在执行代码时执行一些长任务就会执行一些长任务。在自己的代码执行时,weakSelf可能为零(希望你使用的是ARC和5.0,所以你已经弄清了弱引用)。

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.

如果你没有真正的弱引用(<5.0,没有ARC,编译器仍然会接受__weak但是没关系)这会导致崩溃。

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

如果对象指针为零,那么使用' - >'访问ivar将导致崩溃,因此您需要确保它不会发生。

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:

即使你像dasblinkenlight写的那样执行代码,如果此时weakSelf将为nil,它会崩溃,假设您在后台线程上调度块然后在块执行之前释放对象,这使得weakSelf nil因此使用' - 来访问它>'会导致崩溃。在这种情况下,我会修改代码如下:

__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.

此外,您可以测试weakSelf是否为零以防止执行中的昂贵任务(如果它没有意义)(对象已被破坏)。但这取决于用例。

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 ).

但是在使用块进行编程时还需要考虑其他情况,例如:您可以拥有一个作业对象实例,其唯一的作用是在后台执行某些任务,在这种情况下,此代码可能会失败,因为您将创建新任务并且它可以在后台线程上执行块之前解除分配,在这种情况下,您应该保留self并且不在对象中保留块(这将阻止保留周期)。

#2


0  

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;
}

#1


5  

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

传递块变量可以是nil,在调用之前检查或在函数启动时添加断言,否则你将崩溃

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 ).

因为你没有保留自我,我们假设你在执行代码时执行一些长任务就会执行一些长任务。在自己的代码执行时,weakSelf可能为零(希望你使用的是ARC和5.0,所以你已经弄清了弱引用)。

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.

如果你没有真正的弱引用(<5.0,没有ARC,编译器仍然会接受__weak但是没关系)这会导致崩溃。

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

如果对象指针为零,那么使用' - >'访问ivar将导致崩溃,因此您需要确保它不会发生。

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:

即使你像dasblinkenlight写的那样执行代码,如果此时weakSelf将为nil,它会崩溃,假设您在后台线程上调度块然后在块执行之前释放对象,这使得weakSelf nil因此使用' - 来访问它>'会导致崩溃。在这种情况下,我会修改代码如下:

__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.

此外,您可以测试weakSelf是否为零以防止执行中的昂贵任务(如果它没有意义)(对象已被破坏)。但这取决于用例。

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 ).

但是在使用块进行编程时还需要考虑其他情况,例如:您可以拥有一个作业对象实例,其唯一的作用是在后台执行某些任务,在这种情况下,此代码可能会失败,因为您将创建新任务并且它可以在后台线程上执行块之前解除分配,在这种情况下,您应该保留self并且不在对象中保留块(这将阻止保留周期)。

#2


0  

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;
}