Skip to content
This repository was archived by the owner on Feb 2, 2023. It is now read-only.

Commit 616e9ff

Browse files
committed
fixed problem with fetchData problem and updated nic cage example
1 parent a758710 commit 616e9ff

3 files changed

Lines changed: 56 additions & 40 deletions

File tree

AsyncDisplayKit/ASVideoNode.mm

Lines changed: 45 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,6 @@
11

22

33
#import "ASVideoNode.h"
4-
#import "ASDisplayNodeInternal.h"
5-
#import "ASDisplayNode+Subclasses.h"
6-
#import "ASDisplayNode+FrameworkPrivate.h"
7-
8-
@interface ASDisplayNode ()
9-
- (void)setInterfaceState:(ASInterfaceState)newState;
10-
@end
114

125
@interface ASVideoNode () {
136
ASDN::RecursiveMutex _lock;
@@ -24,13 +17,16 @@ @interface ASVideoNode () {
2417

2518
@end
2619

20+
@interface ASDisplayNode ()
21+
- (void)setInterfaceState:(ASInterfaceState)newState;
22+
@end
23+
2724
@implementation ASVideoNode
2825

2926
- (instancetype)init {
3027
if (!(self = [super init])) { return nil; }
3128

3229
_playerNode = [[ASDisplayNode alloc] initWithLayerBlock:^CALayer *{ return [[AVPlayerLayer alloc] init]; }];
33-
3430
[self addSubnode:_playerNode];
3531

3632
self.gravity = ASVideoGravityResizeAspect;
@@ -114,7 +110,7 @@ - (instancetype)initWithViewBlock:(ASDisplayNodeViewBlock)viewBlock didLoadBlock
114110
- (void)fetchData
115111
{
116112
[super fetchData];
117-
113+
118114
@try {
119115
[_currentItem removeObserver:self forKeyPath:NSStringFromSelector(@selector(status))];
120116
}
@@ -124,18 +120,46 @@ - (void)fetchData
124120

125121
{
126122
ASDN::MutexLocker l(_lock);
127-
128123
_currentItem = [[AVPlayerItem alloc] initWithAsset:_asset];
129124
[_currentItem addObserver:self forKeyPath:NSStringFromSelector(@selector(status)) options:NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew context:NULL];
130125

131-
[((AVPlayerLayer *)_playerNode.layer).player replaceCurrentItemWithPlayerItem:_currentItem];
126+
if (((AVPlayerLayer *)_playerNode.layer).player) {
127+
[((AVPlayerLayer *)_playerNode.layer).player replaceCurrentItemWithPlayerItem:_currentItem];
128+
} else {
129+
((AVPlayerLayer *)_playerNode.layer).player = [[AVPlayer alloc] initWithPlayerItem:_currentItem];
130+
}
131+
132132
}
133-
134133
if (_shouldAutoPlay) {
135134
[self play];
136135
}
137136
}
138137

138+
//- (void)fetchData
139+
//{
140+
// [super fetchData];
141+
//
142+
// @try {
143+
// [_currentItem removeObserver:self forKeyPath:NSStringFromSelector(@selector(status))];
144+
// }
145+
// @catch (NSException * __unused exception) {
146+
// NSLog(@"unnecessary removal in fetch data");
147+
// }
148+
//
149+
// {
150+
// ASDN::MutexLocker l(_lock);
151+
//
152+
// _currentItem = [[AVPlayerItem alloc] initWithAsset:_asset];
153+
// [_currentItem addObserver:self forKeyPath:NSStringFromSelector(@selector(status)) options:NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew context:NULL];
154+
//
155+
// [((AVPlayerLayer *)_playerNode.layer).player replaceCurrentItemWithPlayerItem:_currentItem];
156+
// }
157+
//
158+
// if (_shouldAutoPlay) {
159+
// [self play];
160+
// }
161+
//}
162+
139163
- (void)clearFetchedData
140164
{
141165
[super clearFetchedData];
@@ -150,7 +174,7 @@ - (void)clearFetchedData
150174
- (void)setPlayButton:(ASButtonNode *)playButton
151175
{
152176
ASDN::MutexLocker l(_lock);
153-
177+
154178
_playButton = playButton;
155179

156180
[self addSubnode:playButton];
@@ -161,7 +185,7 @@ - (void)setPlayButton:(ASButtonNode *)playButton
161185
- (ASButtonNode *)playButton
162186
{
163187
ASDN::MutexLocker l(_lock);
164-
188+
165189
return _playButton;
166190
}
167191

@@ -188,7 +212,7 @@ - (AVAsset *)asset
188212

189213
- (void)setGravity:(ASVideoGravity)gravity {
190214
ASDN::MutexLocker l(_lock);
191-
215+
192216
switch (gravity) {
193217
case ASVideoGravityResize:
194218
((AVPlayerLayer *)_playerNode.layer).videoGravity = AVLayerVideoGravityResize;
@@ -208,7 +232,7 @@ - (void)setGravity:(ASVideoGravity)gravity {
208232
- (ASVideoGravity)gravity;
209233
{
210234
ASDN::MutexLocker l(_lock);
211-
235+
212236
if ([((AVPlayerLayer *)_playerNode.layer).contentsGravity isEqualToString:AVLayerVideoGravityResize]) {
213237
return ASVideoGravityResize;
214238
}
@@ -222,7 +246,7 @@ - (ASVideoGravity)gravity;
222246
- (void)play;
223247
{
224248
ASDN::MutexLocker l(_lock);
225-
249+
226250
[[((AVPlayerLayer *)_playerNode.layer) player] play];
227251
_shouldBePlaying = YES;
228252
_playButton.alpha = 0.0;
@@ -231,7 +255,7 @@ - (void)play;
231255
UIActivityIndicatorView *spinnnerView = [[UIActivityIndicatorView alloc] initWithFrame:_playButton.frame];
232256
spinnnerView.color = [UIColor whiteColor];
233257
[spinnnerView startAnimating];
234-
258+
235259
return spinnnerView;
236260
}];
237261

@@ -246,8 +270,8 @@ - (BOOL)ready;
246270

247271
- (void)pause;
248272
{
249-
// ASDN::MutexLocker l(_lock);
250-
273+
ASDN::MutexLocker l(_lock);
274+
251275
[[((AVPlayerLayer *)_playerNode.layer) player] pause];
252276
_shouldBePlaying = NO;
253277
_playButton.alpha = 1.0;
@@ -265,3 +289,4 @@ - (void)dealloc
265289
}
266290

267291
@end
292+

examples/Nic Cage TableView/Sample/BlurbNode.m

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,8 @@ - (instancetype)init
4848
_textNode.linkAttributeNames = @[ kLinkAttributeName ];
4949

5050
// generate an attributed string using the custom link attribute specified above
51-
NSString *blurb = @"kittens courtesy placekitten.com \U0001F638";
51+
NSString *blurb = @"Nic Cage courtesy of himself.";
5252
NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:blurb];
53-
[string addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"HelveticaNeue-Light" size:16.0f] range:NSMakeRange(0, blurb.length)];
54-
[string addAttributes:@{
55-
kLinkAttributeName: [NSURL URLWithString:@"http://placekitten.com/"],
56-
NSForegroundColorAttributeName: [UIColor grayColor],
57-
NSUnderlineStyleAttributeName: @(NSUnderlineStyleSingle | NSUnderlinePatternDot),
58-
}
59-
range:[blurb rangeOfString:@"placekitten.com"]];
6053
_textNode.attributedString = string;
6154

6255
// add it as a subnode, and we're done

examples/Nic Cage TableView/Sample/ViewController.m

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818
#import "NicCageNode.h"
1919

2020

21-
static const NSInteger kLitterSize = 20; // intial number of kitten cells in ASTableView
22-
static const NSInteger kLitterBatchSize = 10; // number of kitten cells to add to ASTableView
23-
static const NSInteger kMaxLitterSize = 100; // max number of kitten cells allowed in ASTableView
21+
static const NSInteger kCageSize = 20; // intial number of Cage cells in ASTableView
22+
static const NSInteger kCageBatchSize = 10; // number of Cage cells to add to ASTableView
23+
static const NSInteger kMaxCageSize = 100; // max number of Cage cells allowed in ASTableView
2424

2525
@interface ViewController () <ASTableViewDataSource, ASTableViewDelegate>
2626
{
@@ -55,11 +55,11 @@ - (instancetype)init
5555
_tableView.asyncDelegate = self;
5656

5757
// populate our "data source" with some random kittens
58-
_kittenDataSource = [self createLitterWithSize:kLitterSize];
58+
_kittenDataSource = [self createLitterWithSize:kCageSize];
5959

6060
_blurbNodeIndexPath = [NSIndexPath indexPathForItem:0 inSection:0];
6161

62-
self.title = @"Kittens";
62+
self.title = @"Nic Cage";
6363
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit
6464
target:self
6565
action:@selector(toggleEditingMode)];
@@ -69,18 +69,16 @@ - (instancetype)init
6969

7070
- (NSMutableArray *)createLitterWithSize:(NSInteger)litterSize
7171
{
72-
NSMutableArray *kittens = [NSMutableArray arrayWithCapacity:litterSize];
72+
NSMutableArray *cages = [NSMutableArray arrayWithCapacity:litterSize];
7373
for (NSInteger i = 0; i < litterSize; i++) {
7474

75-
// placekitten.com will return the same kitten picture if the same pixel height & width are requested,
76-
// so generate kittens with different width & height values.
7775
u_int32_t deltaX = arc4random_uniform(10) - 5;
7876
u_int32_t deltaY = arc4random_uniform(10) - 5;
7977
CGSize size = CGSizeMake(350 + 2 * deltaX, 350 + 4 * deltaY);
8078

81-
[kittens addObject:[NSValue valueWithCGSize:size]];
79+
[cages addObject:[NSValue valueWithCGSize:size]];
8280
}
83-
return kittens;
81+
return cages;
8482
}
8583

8684
- (void)setKittenDataSource:(NSMutableArray *)kittenDataSource {
@@ -160,7 +158,7 @@ - (void)tableViewUnlockDataSource:(ASTableView *)tableView
160158

161159
- (BOOL)shouldBatchFetchForTableView:(UITableView *)tableView
162160
{
163-
return _kittenDataSource.count < kMaxLitterSize;
161+
return _kittenDataSource.count < kMaxCageSize;
164162
}
165163

166164
- (void)tableView:(UITableView *)tableView willBeginBatchFetchWithContext:(ASBatchContext *)context
@@ -170,7 +168,7 @@ - (void)tableView:(UITableView *)tableView willBeginBatchFetchWithContext:(ASBat
170168
dispatch_async(dispatch_get_main_queue(), ^{
171169

172170
// populate a new array of random-sized kittens
173-
NSArray *moarKittens = [self createLitterWithSize:kLitterBatchSize];
171+
NSArray *moarKittens = [self createLitterWithSize:kCageBatchSize];
174172

175173
NSMutableArray *indexPaths = [[NSMutableArray alloc] init];
176174

0 commit comments

Comments
 (0)