Skip to content

Commit bdbcb5b

Browse files
committed
Improves UITableView and UICollection view bindings behavior.
1 parent 4b602fd commit bdbcb5b

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

RxCocoa/iOS/UICollectionView+Rx.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,14 @@ extension UICollectionView {
142142
-> (source: O)
143143
-> Disposable {
144144
return { source in
145-
145+
// This is called for sideeffects only, and to make sure delegate proxy is in place when
146+
// data source is being bound.
147+
// This is needed because theoretically the data source subscription itself might
148+
// call `self.rx_delegate`. If that happens, it might cause weird side effects since
149+
// setting data source will set delegate, and UITableView might get into a weird state.
150+
// Therefore it's better to set delegate proxy first, just to be sure.
151+
_ = self.rx_delegate
152+
// Strong reference is needed because data source is in use until result subscription is disposed
146153
return source.subscribeProxyDataSourceForObject(self, dataSource: dataSource, retainDataSource: true) { [weak self] (_: RxCollectionViewDataSourceProxy, event) -> Void in
147154
guard let collectionView = self else {
148155
return

RxCocoa/iOS/UITableView+Rx.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,14 @@ extension UITableView {
145145
-> (source: O)
146146
-> Disposable {
147147
return { source in
148-
// There needs to be a strong retaining here because
148+
// This is called for sideeffects only, and to make sure delegate proxy is in place when
149+
// data source is being bound.
150+
// This is needed because theoretically the data source subscription itself might
151+
// call `self.rx_delegate`. If that happens, it might cause weird side effects since
152+
// setting data source will set delegate, and UITableView might get into a weird state.
153+
// Therefore it's better to set delegate proxy first, just to be sure.
154+
_ = self.rx_delegate
155+
// Strong reference is needed because data source is in use until result subscription is disposed
149156
return source.subscribeProxyDataSourceForObject(self, dataSource: dataSource, retainDataSource: true) { [weak self] (_: RxTableViewDataSourceProxy, event) -> Void in
150157
guard let tableView = self else {
151158
return

0 commit comments

Comments
 (0)