Skip to content

Commit

Permalink
[ASNodeController+Beta] Provide an option to allow nodes to own their…
Browse files Browse the repository at this point in the history
… controllers. (#61)

* [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.

* [ASNodeController] Fix one of the #if's.
  • Loading branch information
appleguy authored and Adlai-Holler committed May 1, 2017
1 parent 4d5e3ce commit 71d5b32
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
8 changes: 8 additions & 0 deletions Source/ASNodeController+Beta.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,18 @@
#import <AsyncDisplayKit/ASDisplayNode.h>
#import <AsyncDisplayKit/ASDisplayNode+Subclasses.h> // 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 <ASInterfaceStateDelegate>

#if INVERT_NODE_CONTROLLER_OWNERSHIP
@property (nonatomic, weak) DisplayNodeType node;
#else
@property (nonatomic, strong) DisplayNodeType node;
#endif

- (void)loadNode;

Expand Down
26 changes: 25 additions & 1 deletion Source/ASNodeController+Beta.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,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;
Expand Down Expand Up @@ -49,6 +70,9 @@ -(void)setNode:(ASDisplayNode *)node
{
_node = node;
_node.interfaceStateDelegate = self;
#if INVERT_NODE_CONTROLLER_OWNERSHIP
_node.asdkNodeController = self;
#endif
}

// subclass overrides
Expand Down

0 comments on commit 71d5b32

Please sign in to comment.