简体   繁体   English

对象的完成块-保留周期?

[英]Completion block of an object - retain cycle?

I know...one more question to this topic. 我知道...对此主题还有另一个问题。 But I really don't get it. 但是我真的不明白。

My code: 我的代码:

//MyViewController.h
@property (strong, nonatomic) UIPageViewController *pageViewController;

//MyViewController.m
- (void)setViewControllerForIndex:(NSInteger)index {
    [self.pageViewController setViewControllers:_myViewControllers[index]
                                      direction:UIPageViewControllerNavigationDirectionForward
                                       animated:animated
                                     completion:^(BOOL finished){

                                            [self updatedViewControllerTo:index]; //this method is doing a lot of stuff

                                     }];
}

Do I have a retain cycle now? 我现在有保留周期吗? self is being captured in the completion block and self has a strong reference to the pageViewController and so indirectly I have a retain cycle? self被捕获在完成块中,并且selfpageViewController有很强的引用,因此间接地我有一个保留周期?

Do I have to use the __weak MyViewController *weakSelf statement and use weakSelf in the completion block? 我是否必须使用__weak MyViewController *weakSelf语句,并在完成块中使用weakSelf

Or: Even when I have a retain cycle does it matter? 或者:即使我有保留周期,这也有关系吗? When the block finishes all the objects in the block will be released and so the retain cycle (or the strong reference to self) will be released, too? 当块完成时,将释放该块中的所有对象,因此也会释放保留周期(或对self的强引用)?

The method updatedViewControllerTo: is doing also UI changes. 方法updatedViewControllerTo:也正在进行UI更改。 So the method has to be called. 因此,必须调用该方法。 When I'm using weakSelf do I have to create a strong reference of self in the block? 当我使用weakSelf ,是否必须在代码块中创建self的强引用? (Referencing to the last code block at http://masteringios.com/blog/2014/03/06/avoid-strong-reference-cycles/ ). (请参阅http://masteringios.com/blog/2014/03/06/avoid-strong-reference-cycles/上的最后一个代码块)。

You do have a retain cycle so please use the weakSelf in your completion block: 您确实有保留周期,因此请在完成代码块中使用weakSelf

Why? 为什么?

Blocks always retain every object that is passed along with them. 块始终保留与它们一起传递的每个对象。 You are strong referencing the pageViewontroller that holds a block that is retaining your viewcontroller. strong引用pageViewontroller ,该pageViewontroller包含一个保留了ViewController的块。

So, these objects are circular referencing eachother so one of them has to have a weak reference. 因此,这些对象是相互循环引用的,因此其中之一必须具有弱引用。 Otherwise, your objects will not be deallocated. 否则,您的对象将不会被释放。

Also you shouldn't worry about the weakSelf reference because: Since you are retaining the pageController in your viewController , it will be 100% certain that when your completionblock gets executed, your "weakSelf" instance is still alive. 另外,您也不必担心weakSelf引用,因为:由于您将pageController保留在viewController ,因此可以100%确定当您完成completeblock时,“ weakSelf”实例仍然有效。

The completion block has a strong reference to self. 完成模块对自我有强烈的参考。 If that completion block is stored away in self then you have a retain cycle. 如果该完成程序块自身存储起来,那么您将有一个保留周期。 Big problem. 大问题。

But if that completion block is not stored away, or in some other object, then you just retain self, but you don't have a retain cycle. 但是,如果未将完成块存储在其他对象中,那么您只是保留了自己,而没有保留周期。 I cannot know how that block is used. 我不知道该块是如何使用的。 If the method being called just does some things, calls the completion block, and returns, then you are absolutely fine. 如果被调用的方法只是做一些事情,调用完成块并返回,那么您绝对可以。

Yours self is retained for block. 您的自我将被保留。 But it isn't problem. 但这不是问题。 Block will be deallocated after invoking and self will be released. 调用后将释放块,并释放自身。

Simple rule: if you release block after invoking you doesn't care about retain cycles in blocks. 一个简单的规则:如果在调用后释放块则不必关心在块中保留循环。

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

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