mirror of
https://github.com/Swiftgram/Telegram-iOS.git
synced 2025-12-23 06:35:51 +00:00
ASTableView batch API and context object
This commit is contained in:
@@ -17,8 +17,10 @@
|
||||
#import "BlurbNode.h"
|
||||
#import "KittenNode.h"
|
||||
|
||||
static const NSInteger kLitterSize = 200;
|
||||
|
||||
static const NSInteger kLitterSize = 20;
|
||||
static const NSInteger kLitterBatchSize = 10;
|
||||
static const NSInteger kMaxLitterSize = 100;
|
||||
|
||||
@interface ViewController () <ASTableViewDataSource, ASTableViewDelegate>
|
||||
{
|
||||
@@ -52,17 +54,23 @@ static const NSInteger kLitterSize = 200;
|
||||
_tableView.asyncDelegate = self;
|
||||
|
||||
// populate our "data source" with some random kittens
|
||||
NSMutableArray *kittenDataSource = [NSMutableArray arrayWithCapacity:kLitterSize];
|
||||
|
||||
_kittenDataSource = [self createLitterWithSize:kLitterSize];;
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
- (NSArray *)createLitterWithSize:(NSInteger)litterSize
|
||||
{
|
||||
NSMutableArray *kittens = [NSMutableArray arrayWithCapacity:litterSize];
|
||||
for (NSInteger i = 0; i < kLitterSize; i++) {
|
||||
u_int32_t deltaX = arc4random_uniform(10) - 5;
|
||||
u_int32_t deltaY = arc4random_uniform(10) - 5;
|
||||
CGSize size = CGSizeMake(350 + 2 * deltaX, 350 + 4 * deltaY);
|
||||
|
||||
[kittenDataSource addObject:[NSValue valueWithCGSize:size]];
|
||||
[kittens addObject:[NSValue valueWithCGSize:size]];
|
||||
}
|
||||
_kittenDataSource = kittenDataSource;
|
||||
|
||||
return self;
|
||||
return kittens;
|
||||
}
|
||||
|
||||
- (void)setKittenDataSource:(NSArray *)kittenDataSource {
|
||||
@@ -117,12 +125,43 @@ static const NSInteger kLitterSize = 200;
|
||||
return NO;
|
||||
}
|
||||
|
||||
- (void)tableViewLockDataSource:(ASTableView *)tableView {
|
||||
- (void)tableViewLockDataSource:(ASTableView *)tableView
|
||||
{
|
||||
self.dataSourceLocked = YES;
|
||||
}
|
||||
|
||||
- (void)tableViewUnlockDataSource:(ASTableView *)tableView {
|
||||
- (void)tableViewUnlockDataSource:(ASTableView *)tableView
|
||||
{
|
||||
self.dataSourceLocked = NO;
|
||||
}
|
||||
|
||||
- (BOOL)shouldBatchFetchForTableView:(UITableView *)tableView
|
||||
{
|
||||
return _kittenDataSource.count < kMaxLitterSize;
|
||||
}
|
||||
|
||||
- (void)tableView:(UITableView *)tableView beginBatchFetchingWithContext:(ASBatchContext *)context
|
||||
{
|
||||
NSLog(@"adding kitties");
|
||||
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
|
||||
sleep(1);
|
||||
dispatch_async(dispatch_get_main_queue(), ^{
|
||||
NSArray *moarKittens = [self createLitterWithSize:kLitterBatchSize];
|
||||
|
||||
NSMutableArray *indexPaths = [[NSMutableArray alloc] init];
|
||||
NSInteger existingKittens = _kittenDataSource.count;
|
||||
for (NSInteger i = 0; i < moarKittens.count; i++) {
|
||||
[indexPaths addObject:[NSIndexPath indexPathForRow:existingKittens + i inSection:0]];
|
||||
}
|
||||
|
||||
_kittenDataSource = [_kittenDataSource arrayByAddingObjectsFromArray:moarKittens];
|
||||
[tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];
|
||||
|
||||
[context completeBatchFetching:YES];
|
||||
|
||||
NSLog(@"kittens added");
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
Reference in New Issue
Block a user