简体   繁体   English

弱自我必要(performSelector)

[英]Is weak self necessary (performSelector)

I have a NSObject category to execute blocks after some interval. 我有一个NSObject类别来在一段时间后执行块。 Do I need to weak self in this instance? 在这种情况下,我是否需要弱自我?

    __weak ViewController *weakSelf = self; 
   [self runBlockAfterDelay:0.6 block:^{

        weakSelf.someview = ...
    }];




 // Category 

- (void)runBlockAfterDelay:(NSTimeInterval)delay block:(void (^)(void))block {

    dispatch_async(dispatch_get_main_queue(), ^{

        [self performSelector:@selector(executeBlockAfterDelay:) withObject:[block copy] afterDelay:delay];
    });
}

- (void)executeBlockAfterDelay:(void(^)(void))block
{
    if (block)
        block();
}

Not only do you not need it (self does not have a strong reference to the block, so there's no cycle), it will likely introduce serious bugs into your program if you include it. 你不仅不需要它(自己没有对块的强引用,所以没有循环),如果你包含它,它可能会给你的程序带来严重的错误。 Specifically, nothing will prevent 'self' from being deallocated before the block runs (since the whole point of a weak reference is that it doesn't prevent things from being deallocated). 具体来说,没有任何东西可以阻止'self'在块运行之前被释放(因为弱引用的整个点是它不会阻止事件被释放)。

Just to add a little more detail to @Catfish_Man's great description: 只是为@ Catfish_Man的精彩描述添加更多细节:

You can weakify self before the block and inside the block you can strongify it again to make sure it is not released in an unexpected time. 您可以在阻止之前弱化自我,在阻止内部,您可以再次强化它以确保它不会在意外时间释放。 In this case it should be ensured that the block is not executed after self has been released. 在这种情况下,应该确保在释放self之后不执行该块。 This is hard, so leaving it strong is a safer option as long as no cycles are created . 这很难, 只要没有创建循环保持强大是一个更安全的选择 (Cycle would be created if self held a reference to the block.) (如果自己持有对块的引用,则会创建循环。)

FYI: If you start thinking in terms of signals instead of procedural ordering, the final result with Reactive Cocoa can be more pleasing: 仅供参考:如果您开始考虑信号而不是程序排序,那么使用Reactive Cocoa的最终结果可能更令人满意:

NSTimeInterval delay = 0.3;
// The activator signal that fires after the delay and completes after it.
RACSignal *delayedActivator = [[RACSignal interval:delay] take:1];

@weakify(self)
// The command that we want to execute after the delay.
RACCommand *blockToExecute = [RACCommand command];
[blockToExecute addSignalBlock:^RACSignal *(id value) {
    @strongify(self)
    self.whatever
}];

// Wire up the command on the signal.
[delayedActivator executeCommand:blockToExecute];

May look a little cryptic at first sight but signals can make your life a whole lot easier. 乍一看可能看起来有点神秘,但信号可以让你的生活变得更加轻松。 Especially because if the signal is tied to the life cycle of self, eg it is a property, it will be released when self is released, this way ensuring that the block is not executed when not needed. 特别是因为如果信号与自身的生命周期相关联,例如它是一个属性,它将在释放self时释放,这样可以确保在不需要时不执行该块。 In this case the weakification and strongification are required. 在这种情况下,需要弱化和强化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 重复自我执行选择器 - Repeating self performSelector “自我”是否必要? - Is “self” necessary? 自我performSelector:@selector(loadData)withObject:nil-不起作用 - self performSelector:@selector(loadData) withObject:nil - not working 取消[self performSelector:... withObject:nil afterDelay:20]; - Cancel [self performSelector:… withObject:nil afterDelay:20]; [self MethodName]和[self performSelector:@selector(Method Name)]之间的区别 - Difference between [self MethodName] and [self performSelector:@selector(Method Name)] 在ARC中总是将自我的弱引用传递给块? - Always pass weak reference of self into block in ARC? 我想使所有[self performSelector:@selector(showLyrics)withObject:nil afterDelay:2]无效; - I want to invalidate all [self performSelector:@selector(showLyrics) withObject:nil afterDelay:2]; 我如何在+(void)classMethod中使用[self performSelector:withObject:afterDelay:]方法 - How can I use [self performSelector: withObject: afterDelay:] method in +(void)classMethod 我需要引用AFNetworkingReachability块中的弱项吗? - Would I need to refer to self with weak in AFNetworkingReachability block 可以在没有__weak对象的块中传递[self anyFunction](iOS 5 + ARC) - Possible to pass [self anyFunction] in blocks without __weak object (iOS 5 + ARC)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM