retain-cycle

Understanding retain cycle in depth

会有一股神秘感。 提交于 2019-11-28 16:25:45
问题 Lets say we have three objects: a grandparent, parent and child. The grandparent retains the parent, the parent retains the child and the child retains the parent. The grandparent releases the parent. What will happen in this case ? 回答1: Unless there is some other reference to the parent or child, they both become orphaned. But the retain cycle between the parent and child prevent either from being released and they become wasted memory. A child should never retain a parent. If anything, use

Weak Reference to NSTimer Target To Prevent Retain Cycle

白昼怎懂夜的黑 提交于 2019-11-28 15:43:06
I'm using an NSTimer like this: timer = [NSTimer scheduledTimerWithTimeInterval:30.0f target:self selector:@selector(tick) userInfo:nil repeats:YES]; Of course, NSTimer retains the target which creates a retain cycle. Furthermore, self isn't a UIViewController so I don't have anything like viewDidUnload where I can invalidate the timer to break the cycle. So I'm wondering if I could use a weak reference instead: __weak id weakSelf = self; timer = [NSTimer scheduledTimerWithTimeInterval:30.0f target:weakSelf selector:@selector(tick) userInfo:nil repeats:YES]; I've heard that the timer must be

Do we need to use __weak self inside UIAnimationBlocks in ARC?

不想你离开。 提交于 2019-11-28 04:44:46
Do we need to use __weak self inside UIAnimation Blocks as given below? Whether it will create retain cycle issue if we are not specifying self as weak? [UIView animateWithDuration:animationDuration delay:0 options:UIViewAnimationCurveEaseInOut animations:^{ [self doSomething]; } completion:^(BOOL finished) { if (finished) { [self doSomething]; } }]; I am also confused in the following scenario. Any thoughts on this? please share your comments. [self.navController dismissViewControllerAnimated:animated completion:^{ [self doSomething]; }]; Should we use weak self here? This is not a retain

Swift closures causing strong retain cycle with self

笑着哭i 提交于 2019-11-28 03:27:14
问题 I just want to know if I am understanding this correctly or not. So according to the apple docs when you create a closure as a property of a class instance and that closure references self(The class that created the closure property) this would cause a strong retain cycle an ultimately the class nor the closure will ever get released. So in laymen terms that means that if I have a class that has a property and that property is a closure, and once I assign the functionality of that closure

Weak Reference to NSTimer Target To Prevent Retain Cycle

怎甘沉沦 提交于 2019-11-27 19:52:24
问题 I'm using an NSTimer like this: timer = [NSTimer scheduledTimerWithTimeInterval:30.0f target:self selector:@selector(tick) userInfo:nil repeats:YES]; Of course, NSTimer retains the target which creates a retain cycle. Furthermore, self isn't a UIViewController so I don't have anything like viewDidUnload where I can invalidate the timer to break the cycle. So I'm wondering if I could use a weak reference instead: __weak id weakSelf = self; timer = [NSTimer scheduledTimerWithTimeInterval:30.0f

Capturing 'self' strongly in this block is likely to lead to a retain cycle [duplicate]

别等时光非礼了梦想. 提交于 2019-11-27 11:40:02
问题 This question already has answers here : capturing self strongly in this block is likely to lead to a retain cycle (7 answers) Closed 5 years ago . I have reqest with block. But the compiler issues a warning "Capturing 'self' strongly in this block is likely to lead to a retain cycle" __weak typeof(self) weakSelf = self; [generalInstaImage setImageWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:data[@"images"][@"low_resolution"][@"url"]]] placeholderImage:[UIImage imageNamed:

How to fix “Capturing 'block' strongly in this block is likely to lead to a retain cycle”

无人久伴 提交于 2019-11-27 11:34:20
问题 I am working on this code, which does some lengthy asyncronous operation on the net and when it finishes it triggers a completion block where some test is executed and if a variable get a certain value another lengthy operation should start immediately: -(void) performOperation { void(^completionBlock) (id obj, NSError *err, NSURLRequest *request)= ^(id obj,NSError *err, NSURLRequest *request){ int variable=0; // Do completion operation A //... //... // Do completion operation B //Get the

Is the weakSelf/strongSelf dance really necessary when referencing self inside a non-retained completion called from a UIViewController?

戏子无情 提交于 2019-11-27 10:29:11
问题 Say I have the following method inside a UIViewController subclass: - (void)makeAsyncNetworkCall { [self.networkService performAsyncNetworkCallWithCompletion:^{ dispatch_async(dispatch_get_main_queue(), ^{ [self.activityIndicatorView stopAnimating]; } }); }]; } I know that the reference to self inside the block results in the UIViewController instance being retained by the block. As long as performAsyncNetworkCallWithCompletion does not store the block in a property (or ivar) on my

Knowing where retain cycles are and removing them

僤鯓⒐⒋嵵緔 提交于 2019-11-27 07:29:44
问题 I was wondering if there was an easy way (or at least a way) to find out where retain cycles exist in your program. Also, if I then know where these retain cycles exist, depending on their types (e.g. variable or closure), how do I make them weak. I need to stop all retain cycles with self (my GameScene) so that it deallocates when I don't need it anymore and I want to restart it. Any tips, advice, answers, and feedback would be greatly appreciated (and providing specific code and examples

Referring to weak self inside a nested block

一曲冷凌霜 提交于 2019-11-27 06:57:52
Suppose I already create a weak self using __weak typeof(self) weakSelf = self; [self doABlockOperation:^{ ... }]; Inside that block, if I nest another block: [weakSelf doAnotherBlockOperation:^{ [weakSelf doSomething]; } will it create a retain cycle? Do I need to create another weak reference to the weakSelf? __weak typeof(self) weakerSelf = weakSelf; [weakSelf doAnotherBlockOperation:^{ [weakerSelf doSomething]; } It depends. You only create a retain cycle if you actually store the block (because self points to the block, and block points to self ). If you don't intend to store either of