From bdbcb5bc87aaab278127ded6625f1a84a16e5dbc Mon Sep 17 00:00:00 2001 From: Krunoslav Zaher Date: Sun, 14 Aug 2016 23:25:54 +0200 Subject: [PATCH] Improves UITableView and UICollection view bindings behavior. --- RxCocoa/iOS/UICollectionView+Rx.swift | 9 ++++++++- RxCocoa/iOS/UITableView+Rx.swift | 9 ++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/RxCocoa/iOS/UICollectionView+Rx.swift b/RxCocoa/iOS/UICollectionView+Rx.swift index 4fee4d70..9e7af88c 100644 --- a/RxCocoa/iOS/UICollectionView+Rx.swift +++ b/RxCocoa/iOS/UICollectionView+Rx.swift @@ -142,7 +142,14 @@ extension UICollectionView { -> (source: O) -> Disposable { return { source in - + // This is called for sideeffects only, and to make sure delegate proxy is in place when + // data source is being bound. + // This is needed because theoretically the data source subscription itself might + // call `self.rx_delegate`. If that happens, it might cause weird side effects since + // setting data source will set delegate, and UITableView might get into a weird state. + // Therefore it's better to set delegate proxy first, just to be sure. + _ = self.rx_delegate + // Strong reference is needed because data source is in use until result subscription is disposed return source.subscribeProxyDataSourceForObject(self, dataSource: dataSource, retainDataSource: true) { [weak self] (_: RxCollectionViewDataSourceProxy, event) -> Void in guard let collectionView = self else { return diff --git a/RxCocoa/iOS/UITableView+Rx.swift b/RxCocoa/iOS/UITableView+Rx.swift index 204186ca..3587c1bb 100644 --- a/RxCocoa/iOS/UITableView+Rx.swift +++ b/RxCocoa/iOS/UITableView+Rx.swift @@ -145,7 +145,14 @@ extension UITableView { -> (source: O) -> Disposable { return { source in - // There needs to be a strong retaining here because + // This is called for sideeffects only, and to make sure delegate proxy is in place when + // data source is being bound. + // This is needed because theoretically the data source subscription itself might + // call `self.rx_delegate`. If that happens, it might cause weird side effects since + // setting data source will set delegate, and UITableView might get into a weird state. + // Therefore it's better to set delegate proxy first, just to be sure. + _ = self.rx_delegate + // Strong reference is needed because data source is in use until result subscription is disposed return source.subscribeProxyDataSourceForObject(self, dataSource: dataSource, retainDataSource: true) { [weak self] (_: RxTableViewDataSourceProxy, event) -> Void in guard let tableView = self else { return