I have a ViewModel for a UITableView that contains an array of data items. UITableView implements pull-to-refresh and infinite scroll behavior. Data items are requested through RestKit from the server paginated, which means that I need to somehow track the current page. I created 2 separate RACCommands to distinguish between updates and endless scrolling, where the update always loads page 0. It all works, but I don't like it, and I want to know if there is a better way to do this. Plus now two commands can be executed simultaneously, which is not intended.
@interface TableDataViewModel ()
@property(nonatomic, readonly) RestApiConnector *rest;
@property(nonatomic) int page;
@property(nonatomic) NSMutableArray *data;
@property(nonatomic) RACCommand *loadCommand;
@property(nonatomic) RACCommand *reloadCommand;
@end
@implementation TableDataViewModel
objection_requires_sel(@selector(rest))
- (id)init {
self = [super init];
if (self) {
[self configureLoadCommands];
[self configureActiveSignal];
}
return self;
}
- (void)configureActiveSignal {
[self.didBecomeActiveSignal subscribeNext:^(id x) {
if (!self.data) {
[self.reloadCommand execute:self];
}
}];
}
- (void)configureLoadCommands {
self.loadCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
return [self.rest dataSignalWithPage:self.page];
}];
self.reloadCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
return [self.rest dataSignalWithPage:self.page];
}];
self.loadCommand.allowsConcurrentExecution = FALSE;
self.reloadCommand.allowsConcurrentExecution = FALSE;
[self.reloadCommand.executionSignals subscribeNext:^(id x) {
RAC(self, data) = [x map:^id(id value) {
self.page = 0;
return [value mutableCopy];
}];
}];
[self.loadCommand.executionSignals subscribeNext:^(id x) {
RAC(self, data) = [x map:^id(id value) {
self.page++;
if (self.data) {
NSMutableArray *array = [self.data mutableCopy];
[array addObjectsFromArray:value];
return array;
} else {
return [value mutableCopy];
}
}];
}];
}
@end
I just started using ReactiveCocoa, so I would appreciate any other advice for this.
Thank!