retain-cycle

ARC, self and blocks

旧巷老猫 提交于 2019-12-20 12:39:27
问题 I thought I understood the usage of self in a block that is copied is a no no . But in an attempt to clean my code i enabled a bunch of warnings in Xcode, one called "Sending messages to weak pointers" so now in all my blocks, every time I use my created weakself reference __weak typeof(self) weakself = self; I get this warning: Weak receiver may be unpredictably set to nil a trivial example: __weak typeof(self) weakself = self; [aClass doSomethingInABlock:^{ [weakself doSomething]; //warning

Using 'self' on RxSwift closures… What about instance methods as param?

白昼怎懂夜的黑 提交于 2019-12-20 03:24:15
问题 In other stack overflow questions, it was emphasized that the capture [weak self] should be used for closures that aren't owned by the class because self could be nil before the closure completes. An alternative when the closure is owned by the class itself is [unowned self] . My question is do I need to use [unowned self] when the function I pass as a parameter is an instance method of the current class? Example import RxSwift class Person { var name = "Default name" class func getPersons()

What happens to Dispatch Queues when UIViewController is Deallocated?

倾然丶 夕夏残阳落幕 提交于 2019-12-18 11:42:42
问题 I am trying to better understand retain cycles, especially relative to Dispatch Queues. I am working with AVFoundation and managing an AVCaptureSession on a sessionQueue: private let sessionQueue = DispatchQueue(label: "com.andrewferrarone.sessionQueue") in a lot of code examples in the apple documentation I see this: self.sessionQueue.async { [unowned self] // } Is the [unowned self] self here necessary? self (the viewController) references self.sessionQueue and the closure dispatched to

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

点点圈 提交于 2019-12-17 17:40:55
问题 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

Is cleaning up strong references in deinit a correct pattern?

可紊 提交于 2019-12-13 12:22:57
问题 There are several resources (blog, SO question, plus I've seen it used everywhere) that recommend removing an observer from the NotificationCenter in the deinit of the UIViewController , e.g.: deinit { NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillShow, object: nil) NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillHide, object: nil) } Now while according to another blog entry I don't have to care about removing

How to properly keep a local reference to a child CCNode in a custom class in ARC

心不动则不痛 提交于 2019-12-13 07:35:21
问题 So, I want to hold a local reference to a child CCNode in a CCNode custom class without creating a retain cycle or without having the compiler complaining that the property or instance variable may be unpredictable set to nil For instance with this custom class @interface MyNode : CCNode @property (nonatomic, strong) CCNode *background; + (id)nodeWithBackground:(id)background; @end @implementation MyNode + (id)nodeWithBackground:(id)background { MyNode *node = [MyNode node]; node.background =

obj-c weak self in a block: why the 2nd one doesn't need a weak self inside in two similar cases

懵懂的女人 提交于 2019-12-11 04:58:41
问题 I finally found my memory bug is caused by referring self strongly in a block. But I don't know why in a similar case, the weak is not needed: I have a CameraCaptureManager class doing image capture tasks, and a CameraViewController has a strong property of this manager. The manager has weak delegate property pointing back to the controller. This is where I must use weakSelf in the manager, otherwise -(void)dealloc won't be called: // in CameraCaptureManager __weak CameraCaptureManager

Why does UIAlertController create a retain cycle with self?

耗尽温柔 提交于 2019-12-10 15:36:57
问题 UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"alert" message:nil preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *action = [UIAlertAction actionWithTitle:@"action" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { [self doSomething]; }]; [alert addAction:action]; [self presentViewController:alert animated:YES completion:nil]; I understand the cycle . self retains the UIAlertController , UIAlertController retains the

Swift Retain Cycles and Closures

僤鯓⒐⒋嵵緔 提交于 2019-12-10 13:56:07
问题 I have tried to do a lot of research on understanding retain cycles. I can't seem to find anything on my examples though. I do know that if i set a property to a closure then a retain cycle happens and need to use weak or unowned. But i have 2 examples that I would like to know if they are done correctly: Thanks in advance, I have tried to see if they are on stackoverflow already but couldn't find anything. Simple animations UIView.transitionWithView(self, duration: 5, options:

How does UIView prevent retain cycle?

我与影子孤独终老i 提交于 2019-12-10 10:38:52
问题 Subview has a reference to superview, while superview also has reference (subviews) to subview. I'm wondering why this doesn't cause retain cycle? 回答1: UIView 's superview property is declared as @property(nonatomic, readonly) UIView *superview; In Objective-C, properties declared without a different ownership specifier are assign by default strong by default as of the introduction of ARC, however, the UIKit headers appear to not be using ARC, so this property is most like assign . Note also,