forked from TextureGroup/Texture
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathViewController.m
More file actions
200 lines (159 loc) · 6.4 KB
/
Copy pathViewController.m
File metadata and controls
200 lines (159 loc) · 6.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
//
// ViewController.m
// Sample
//
// Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree. An additional grant
// of patent rights can be found in the PATENTS file in the same directory.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
#import "ViewController.h"
#import <AsyncDisplayKit/AsyncDisplayKit.h>
#import <AsyncDisplayKit/ASAssert.h>
#define NumberOfSections 10
#define NumberOfRowsPerSection 20
#define NumberOfReloadIterations 50
typedef enum : NSUInteger {
ReloadData,
ReloadRows,
ReloadSections,
ReloadTypeMax
} ReloadType;
@interface ViewController () <ASTableViewDataSource, ASTableViewDelegate>
{
ASTableView *_tableView;
NSMutableArray *_sections; // Contains arrays of indexPaths representing rows
}
@end
@implementation ViewController
- (instancetype)init
{
if (!(self = [super init]))
return nil;
_tableView = [[ASTableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
_tableView.asyncDataSource = self;
_tableView.asyncDelegate = self;
_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
_sections = [NSMutableArray arrayWithCapacity:NumberOfSections];
for (int i = 0; i < NumberOfSections; i++) {
NSMutableArray *rowsArray = [NSMutableArray arrayWithCapacity:NumberOfRowsPerSection];
for (int j = 0; j < NumberOfRowsPerSection; j++) {
[rowsArray addObject:[NSIndexPath indexPathForRow:j inSection:i]];
}
[_sections addObject:rowsArray];
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self.view addSubview:_tableView];
}
- (void)viewWillLayoutSubviews
{
_tableView.frame = self.view.bounds;
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self thrashTableView];
}
- (NSIndexSet *)randomIndexSet
{
u_int32_t upperBound = (u_int32_t)_sections.count - 1;
u_int32_t randA = arc4random_uniform(upperBound);
u_int32_t randB = arc4random_uniform(upperBound);
return [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(MIN(randA, randB), MAX(randA, randB) - MIN(randA, randB))];
}
- (NSArray *)randomIndexPathsExisting:(BOOL)existing
{
NSMutableArray *indexPaths = [NSMutableArray array];
[[self randomIndexSet] enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) {
NSUInteger rowNum = [self tableView:_tableView numberOfRowsInSection:idx];
NSIndexPath *sectionIndex = [[NSIndexPath alloc] initWithIndex:idx];
for (NSUInteger i = (existing ? 0 : rowNum); i < (existing ? rowNum : rowNum * 2); i++) {
// Maximize evility by sporadically skipping indicies 1/3rd of the time, but only if reloading existing rows
if (existing && arc4random_uniform(2) == 0) {
continue;
}
NSIndexPath *indexPath = [sectionIndex indexPathByAddingIndex:i];
[indexPaths addObject:indexPath];
}
}];
return indexPaths;
}
- (void)thrashTableView
{
[_tableView reloadData];
NSArray *indexPathsAddedAndRemoved = nil;
for (int i = 0; i < NumberOfReloadIterations; ++i) {
UITableViewRowAnimation rowAnimation = (arc4random_uniform(1) == 0 ? UITableViewRowAnimationMiddle : UITableViewRowAnimationNone);
BOOL animatedScroll = (arc4random_uniform(2) == 0 ? YES : NO);
ReloadType reloadType = (arc4random_uniform(ReloadTypeMax));
BOOL letRunloopProceed = (arc4random_uniform(2) == 0 ? YES : NO);
BOOL useBeginEndUpdates = (arc4random_uniform(3) == 0 ? YES : NO);
// FIXME: Need to revise the logic to support mutating the data source rather than just reload thrashing.
// UITableView itself does not support deleting a row in the same edit transaction as reloading it, for example.
BOOL addIndexPaths = NO; //(arc4random_uniform(2) == 0 ? YES : NO);
if (useBeginEndUpdates) {
[_tableView beginUpdates];
}
switch (reloadType) {
case ReloadData:
[_tableView reloadData];
break;
case ReloadRows:
[_tableView reloadRowsAtIndexPaths:[self randomIndexPathsExisting:YES] withRowAnimation:rowAnimation];
break;
case ReloadSections:
[_tableView reloadSections:[self randomIndexSet] withRowAnimation:rowAnimation];
break;
default:
break;
}
if (addIndexPaths && !indexPathsAddedAndRemoved) {
indexPathsAddedAndRemoved = [self randomIndexPathsExisting:NO];
for (NSIndexPath *indexPath in indexPathsAddedAndRemoved) {
[_sections[indexPath.section] addObject:indexPath];
}
[_tableView insertRowsAtIndexPaths:indexPathsAddedAndRemoved withRowAnimation:rowAnimation];
}
[_tableView setContentOffset:CGPointMake(0, arc4random_uniform(_tableView.contentSize.height - _tableView.bounds.size.height)) animated:animatedScroll];
if (letRunloopProceed) {
// Run other stuff on the main queue for between 2ms and 1000ms.
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:(1 / (1 + arc4random_uniform(500)))]];
if (indexPathsAddedAndRemoved) {
for (NSIndexPath *indexPath in indexPathsAddedAndRemoved) {
[_sections[indexPath.section] removeObjectIdenticalTo:indexPath];
}
[_tableView deleteRowsAtIndexPaths:indexPathsAddedAndRemoved withRowAnimation:rowAnimation];
indexPathsAddedAndRemoved = nil;
}
}
if (useBeginEndUpdates) {
[_tableView endUpdates];
}
}
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return _sections.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [(NSArray *)[_sections objectAtIndex:section] count];
}
- (ASCellNode *)tableView:(ASTableView *)tableView nodeForRowAtIndexPath:(NSIndexPath *)indexPath
{
ASTextCellNode *textCellNode = [ASTextCellNode new];
textCellNode.text = indexPath.description;
return textCellNode;
}
@end