From 4aaba07121a83c5df2f6d5b48b0028fba3bb42ed Mon Sep 17 00:00:00 2001 From: Scott Goodson Date: Sun, 23 Apr 2017 14:51:58 -0700 Subject: [PATCH 1/2] [ASNodeController+Beta] Provide an option to allow nodes to own their controllers. We should certainly remove this before moving ASNodeController out of Beta. However, I think it will require at least ASCollectionNode to be able to retain its top level set of node controllers. Without this facility built in, it's very difficult for apps supporting both UIKit and ASDK to manually manage the controllers and keep them in sync with perfect timing. --- Source/ASNodeController+Beta.h | 8 ++++++++ Source/ASNodeController+Beta.m | 26 +++++++++++++++++++++++++- 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/Source/ASNodeController+Beta.h b/Source/ASNodeController+Beta.h index 05a859792..02a3f801c 100644 --- a/Source/ASNodeController+Beta.h +++ b/Source/ASNodeController+Beta.h @@ -9,10 +9,18 @@ #import #import // for ASInterfaceState protocol +// Until an ASNodeController can be provided in place of an ASCellNode, some apps may prefer to have +// nodes keep their controllers alive (and a weak reference from controller to node) +#define INVERT_NODE_CONTROLLER_OWNERSHIP 0 + /* ASNodeController is currently beta and open to change in the future */ @interface ASNodeController<__covariant DisplayNodeType : ASDisplayNode *> : NSObject +#if INVERT_NODE_CONTROLLER_OWNERSHIP @property (nonatomic, strong) DisplayNodeType node; +#else +@property (nonatomic, weak) DisplayNodeType node; +#endif - (void)loadNode; diff --git a/Source/ASNodeController+Beta.m b/Source/ASNodeController+Beta.m index 2fd01497b..afbce9996 100644 --- a/Source/ASNodeController+Beta.m +++ b/Source/ASNodeController+Beta.m @@ -7,9 +7,30 @@ // #import "ASNodeController+Beta.h" - #import "ASDisplayNode+FrameworkPrivate.h" +#if INVERT_NODE_CONTROLLER_OWNERSHIP + +@interface ASDisplayNode (ASNodeController) +@property (nonatomic, strong) ASNodeController *asdkNodeController; +@end + +@implementation ASDisplayNode (ASNodeController) + +- (ASNodeController *)asdkNodeController +{ + return objc_getAssociatedObject(self, @selector(asdkNodeController)); +} + +- (void)setAsdkNodeController:(ASNodeController *)asdkNodeController +{ + objc_setAssociatedObject(self, @selector(asdkNodeController), asdkNodeController, OBJC_ASSOCIATION_RETAIN_NONATOMIC); +} + +@end + +#endif + @implementation ASNodeController @synthesize node = _node; @@ -40,6 +61,9 @@ -(void)setNode:(ASDisplayNode *)node { _node = node; _node.interfaceStateDelegate = self; +#if INVERT_NODE_CONTROLLER_OWNERSHIP + _node.asdkNodeController = self; +#endif } // subclass overrides From 79448bc42ace904326e0516e284e2dac18e29718 Mon Sep 17 00:00:00 2001 From: Scott Goodson Date: Wed, 26 Apr 2017 19:44:07 -0700 Subject: [PATCH 2/2] [ASNodeController] Fix one of the #if's. --- Source/ASNodeController+Beta.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Source/ASNodeController+Beta.h b/Source/ASNodeController+Beta.h index 3ce65a8d8..7c75c5eec 100644 --- a/Source/ASNodeController+Beta.h +++ b/Source/ASNodeController+Beta.h @@ -26,9 +26,9 @@ @interface ASNodeController<__covariant DisplayNodeType : ASDisplayNode *> : NSObject #if INVERT_NODE_CONTROLLER_OWNERSHIP -@property (nonatomic, strong) DisplayNodeType node; -#else @property (nonatomic, weak) DisplayNodeType node; +#else +@property (nonatomic, strong) DisplayNodeType node; #endif - (void)loadNode;