Search code examples
iosobjective-cuicollectionviewreactive-cocoa

Implement UICollectionViewDataSource methods with reactivecocoa


I am trying to learn RAC, and I've already seen how one can implement delegate methods using rac_signalForSelector. I am using a UICollectionView to display some data, and I've handled all the necessary UICollectionViewDelegate methods using this solution. For example, here is a collectionView:didSelectItemAtIndexPath: implementation:

[[weakSelf rac_signalForSelector:@selector(collectionView:didSelectItemAtIndexPath:) fromProtocol:@protocol(UICollectionViewDelegate)] subscribeNext:^(RACTuple *arguments) {
        __strong MainViewController *strongSelf = weakSelf;
        NSIndexPath *indexPath = arguments.second;
        DetailViewModel *viewModel = [[DetailViewModel alloc] initWithModel:strongSelf.viewModel.model[indexPath.item]];
        DetailViewController *viewController = [[DetailViewController alloc] initWithViewModel:viewModel];
        [strongSelf.navigationController pushViewController:viewController animated:YES];
}];

I am wondering if it is possible to implement UICollectionViewDataSource methods like this as well, since they also have return values.


Solution

  • The short answer - NO

    Reactive Cocoa is a reactive framework and you should design your app logic in a reactive way not proactive. It means that you should react to some events and do something about it.
    As an example - user click on a cell and you get an RAC event, you can manipulate that event (map, filter, combine with other events and etc).

    It not possible because Reactive Cocoa is designed for push-based API for composing and transforming streams of values.