Tag: 保留循环

在自UIViewController调用的非保留完成中引用self时,weakSelf / strongSelf舞真的有必要吗?

说我在UIViewController子类中有以下方法: – (void)makeAsyncNetworkCall { [self.networkService performAsyncNetworkCallWithCompletion:^{ dispatch_async(dispatch_get_main_queue(), ^{ [self.activityIndicatorView stopAnimating]; } }); }]; } 我知道块内的self引用导致UIViewController实例被块保留。 只要performAsyncNetworkCallWithCompletion不会将该块存储在我的NetworkService的属性(或ivar)上,我是否认为没有保留周期? 我意识到上面的这个结构将导致UIViewController被保留,直到performAsyncNetworkCallWithCompletion完成,即使它是由系统提前发布的。 但它可能(甚至可能?)系统将释放我的UIViewController 在所有 ( iOS 6pipe理UIViewController的支持CALayer内存的方式更改后 )? 如果有一个原因,我必须做的“自我/自我舞蹈”,看起来是这样的: – (void)makeAsyncNetworkCall { __weak typeof(self) weakSelf = self; [self.networkService performAsyncNetworkCallWithCompletion:^{ typeof(weakSelf) strongSelf = weakSelf; if (!strongSelf) { return; } dispatch_async(dispatch_get_main_queue(), ^{ [strongSelf.activityIndicatorView stopAnimating]; } }); }]; } 但是我觉得这很丑陋,如果没有必要,我想避免它。

在dispatch_async函数中使用weak self

我读了很多关于在dispatch_async里面使用__weak self的post,现在我有点困惑了。 如果我有 : self.myQueue = dispatch_queue_create("com.biview.core_data", NULL); dispatch_async(self.myQueue, ^(void){ if (!self.var1) { self.var1 = …; } dispatch_async(dispatch_get_main_queue(), ^(void) { if ([self.var2 superview]) { [self.var2 removeFromSuperview]; } [self.Label setText:text]; }); }); 我是否需要使用__weak self 。 因为我在某些情况下读到dispatch_async不需要__weak self 。 在这里看最后的评论

深入了解保留周期

假设我们有三个对象:祖父母,父母和孩子。 祖父母保留父母,父母保留孩子,孩子保留父母。 祖父母释放父母。 在这种情况下会发生什么?