简体   繁体   English

__弱的通用变量声明块

[英]__weak general variables declaration for blocks

I am currently working on an implementation where many blocks are used. 我目前正在研究使用许多块的实现。 Every block needs to communicate with self. 每个障碍都需要与自我沟通。

Currently I am doing this: 目前,我正在这样做:

@implementation Foo
- (void) bar
{
    __weak Foo *weakSelf = self;
    [self doBlockStuff:^(id something) {
        [weakSelf doSomething];
    }];
}
@end

I have many functions that do the same with the weak instantiation. 我有许多功能与弱实例化相同。

Is it right to instantiate the weak property once in the interface block and use it everywhere? 在接口块中实例化弱属性一次并在各处使用它是否正确?

It's working but is it an accepted practice? 它正在工作,但是被接受吗?

@interface Foo ()
{
    __weak Foo *_weakSelf;  
}
@end

@implementation Foo
-(instancetype) init
{
    self = [super init];
    if(self) {
        _weakSelf = self;
    }
    return self;
}

- (void) bar1
{
    [self doBlockStuff:^(id something) {
        [_weakSelf doSomething];
    }];
}
- (void) bar2
{
    [self doBlockStuff:^(id something) {
        [_weakSelf doSomething];
    }];
}
- (void) bar3
{
    [self doBlockStuff:^(id something) {
        [_weakSelf doSomething];
    }];
}
- (void) bar4
{
    [self doBlockStuff:^(id something) {
        [_weakSelf doSomething];
    }];
}
@end

Edit after Testing with new Informations: I did wrote a little test case and now i can demonstrate why the second one is not working. 使用新信息进行测试后进行编辑:我确实编写了一个小测试用例,现在我可以演示为什么第二个无法使用。 In my Testclass a imake a dispatch after 5 seconds with the relevant self usage and i logged when my dealloc was called. 在我的Testclass中,在5秒后使用相关的自用信息发出了一个调度,当我调用dealloc时我记录了日志。

@implementation Foo

- (void)dealloc
{
    NSLog(@"dealloc");
}

- (instancetype)init
{
    self = [super init];
    if (self) {

    }

    return;
}

- (void)bar
{

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self doSomething];
    });
}

@end

If the class loses the holder, because the controller is closed or whatever and the function is still running, the class will dialoged after the dispatch is done. 如果该类丢失了所有者,因为控制器已关闭或其他原因并且该函数仍在运行,则在分派完成后,该类将进行对话。

@interface Foo ()
{
    __weak Foo *_weakSelf;  
}
@end

@implementation Foo

- (void)dealloc
{
    NSLog(@"dealloc");
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        _weakSelf = self;
    }

    return;
}

- (void)bar
{

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [_weakSelf doSomething];
    });
}

@end

This one will also on dealloc if the dispatch is done. 如果完成了分配,则这也会在dealloc上进行。 Because the _weakSelf property is still holing by the class, a shorthand for using self->_weak. 由于_weakSelf属性仍在类中钻研,因此是使用self-> _ weak的简写。 Self means self :) 自我意味着自我:)

@implementation Foo

- (void)dealloc
{
    NSLog(@"dealloc");
}

- (instancetype)init
{
    self = [super init];
    if (self) {

    }

    return;
}

- (void)bar
{
     __weak typeof(self) weakSelf = self;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [weakSelf doSomething];
    });
}

@end

this one will dealloc immediately because the weak reference is only existing in this function given to the block. 由于弱引用仅存在于分配给该块的该函数中,因此该函数将立即取消分配。 The function is over and if the class loses his reference the block has no property that is holing anyone. 该函数已结束,如果该类丢失了其引用,则该块将不具有阻碍任何人的属性。 But the weak property is still usable when the reference class is available. 但是,当引用类可用时,弱属性仍然可用。 To be sure, that this weak property will be alive, we can set a strong cycle in the block. 可以肯定的是,该弱属性将仍然存在,我们可以在该块中设置一个强周期。

This doesn't at all do what you think it does. 这根本不符合您的想法。 That __weak instance variable in those methods? 这些方法中的__weak实例变量? That's just a shorthand for self->_weak . 那只是self->_weak的简写。 All of those methods using the proposed manner still capture self strongly. 使用建议的方式的所有这些方法仍然可以很强地self捕捉。

Stick to what you were doing before. 坚持以前的做法。

This is really bad then if the __weakSelf still holds a strong reference. 如果__weakSelf仍然拥有很强的引用,那么这真的很糟糕。 The question is that if instantiated in the method and used in the method does it still really have a weak anchor to self or is it holding a strong reference in that very moment. 问题是,如果在该方法中实例化并在该方法中使用它,它对自身的锚定是否仍然很弱,或者在那一刻它是否拥有强大的参照力。 Based on the documentation you can instantiate a weak reference outside the block and even make it strong inside the block if you want. 根据文档,您可以在块外实例化一个弱引用,如果需要,甚至可以在块内实例化一个弱引用。 Take a look here 在这里看看

http://aceontech.com/objc/ios/2014/01/10/weakify-a-more-elegant-solution-to-weakself.html http://aceontech.com/objc/ios/2014/01/10/weakify-a-more-elegant-solution-to-weakself.html

my question at this point in time is that why does a weak self instantiated dealloc it's self when the real self deallocs..? 我此时的问题是,为什么当真实的self进行dealloc时,弱的self实例化将其self进行dealloc。 Because I tried to hold the weak self in another viewController and then dealloc the real self. 因为我试图将弱的自身保留在另一个viewController中,然后取消分配实际的自我。 But the weak self was dealloc as soon as I dealloc the real self. 但是,一旦我解除了真实自我的分配,弱者就被解除了分配。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM