Skip to content

Commit bf7aaeb

Browse files
Mikhail Markinkzaher
authored andcommitted
DispatchQueue.main.async removed for RxCollectionViewSectionedAnimatedDataSource and RxTableViewSectionedAnimatedDataSource.
batchUpdates(_:animationConfiguration:) removed from SectionedViewType protocol interface. self replaced with dataSource for tableView(_:observeEvent:) and collectionView(_:observeEvent:). indentation fixed.
1 parent ad0690d commit bf7aaeb

5 files changed

+72
-77
lines changed

Sources/RxDataSources/RxCollectionViewSectionedAnimatedDataSource.swift

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -52,49 +52,48 @@ open class RxCollectionViewSectionedAnimatedDataSource<S: AnimatableSectionModel
5252
open func collectionView(_ collectionView: UICollectionView, observedEvent: Event<Element>) {
5353
Binder(self) { dataSource, newSections in
5454
#if DEBUG
55-
self._dataSourceBound = true
55+
dataSource._dataSourceBound = true
5656
#endif
57-
if !self.dataSet {
58-
self.dataSet = true
57+
if !dataSource.dataSet {
58+
dataSource.dataSet = true
5959
dataSource.setSections(newSections)
6060
collectionView.reloadData()
6161
}
6262
else {
63-
DispatchQueue.main.async {
64-
// if view is not in view hierarchy, performing batch updates will crash the app
65-
if collectionView.window == nil {
66-
dataSource.setSections(newSections)
67-
collectionView.reloadData()
68-
return
69-
}
70-
let oldSections = dataSource.sectionModels
71-
do {
72-
let differences = try Diff.differencesForSectionedView(initialSections: oldSections, finalSections: newSections)
73-
74-
switch self.decideViewTransition(self, collectionView, differences) {
75-
case .animated:
76-
// each difference must be run in a separate 'performBatchUpdates', otherwise it crashes.
77-
// this is a limitation of Diff tool
78-
for difference in differences {
79-
collectionView.performBatchUpdates({ [unowned self] in
80-
// sections must be set within updateBlock in 'performBatchUpdates'
81-
dataSource.setSections(difference.finalSections)
82-
collectionView.batchUpdates(difference, animationConfiguration: self.animationConfiguration)
83-
}, completion: nil)
63+
// if view is not in view hierarchy, performing batch updates will crash the app
64+
if collectionView.window == nil {
65+
dataSource.setSections(newSections)
66+
collectionView.reloadData()
67+
return
68+
}
69+
let oldSections = dataSource.sectionModels
70+
do {
71+
let differences = try Diff.differencesForSectionedView(initialSections: oldSections, finalSections: newSections)
72+
73+
switch dataSource.decideViewTransition(dataSource, collectionView, differences) {
74+
case .animated:
75+
// each difference must be run in a separate 'performBatchUpdates', otherwise it crashes.
76+
// this is a limitation of Diff tool
77+
for difference in differences {
78+
let updateBlock = {
79+
// sections must be set within updateBlock in 'performBatchUpdates'
80+
dataSource.setSections(difference.finalSections)
81+
collectionView.batchUpdates(difference, animationConfiguration: dataSource.animationConfiguration)
8482
}
85-
86-
case .reload:
87-
self.setSections(newSections)
88-
collectionView.reloadData()
89-
return
83+
collectionView.performBatchUpdates(updateBlock, completion: nil)
9084
}
91-
}
92-
catch let e {
93-
rxDebugFatalError(e)
94-
self.setSections(newSections)
85+
86+
case .reload:
87+
dataSource.setSections(newSections)
9588
collectionView.reloadData()
89+
return
9690
}
9791
}
92+
catch let e {
93+
rxDebugFatalError(e)
94+
dataSource.setSections(newSections)
95+
collectionView.reloadData()
96+
}
9897
}
9998
}.on(observedEvent)
10099
}

Sources/RxDataSources/RxCollectionViewSectionedReloadDataSource.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ open class RxCollectionViewSectionedReloadDataSource<S: SectionModelType>
2424
open func collectionView(_ collectionView: UICollectionView, observedEvent: Event<Element>) {
2525
Binder(self) { dataSource, element in
2626
#if DEBUG
27-
self._dataSourceBound = true
27+
dataSource._dataSourceBound = true
2828
#endif
2929
dataSource.setSections(element)
3030
collectionView.reloadData()

Sources/RxDataSources/RxTableViewSectionedAnimatedDataSource.swift

Lines changed: 38 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -78,56 +78,54 @@ open class RxTableViewSectionedAnimatedDataSource<S: AnimatableSectionModelType>
7878
open func tableView(_ tableView: UITableView, observedEvent: Event<Element>) {
7979
Binder(self) { dataSource, newSections in
8080
#if DEBUG
81-
self._dataSourceBound = true
81+
dataSource._dataSourceBound = true
8282
#endif
83-
if !self.dataSet {
84-
self.dataSet = true
83+
if !dataSource.dataSet {
84+
dataSource.dataSet = true
8585
dataSource.setSections(newSections)
8686
tableView.reloadData()
8787
}
8888
else {
89-
DispatchQueue.main.async {
90-
// if view is not in view hierarchy, performing batch updates will crash the app
91-
if tableView.window == nil {
92-
dataSource.setSections(newSections)
93-
tableView.reloadData()
94-
return
95-
}
96-
let oldSections = dataSource.sectionModels
97-
do {
98-
let differences = try Diff.differencesForSectionedView(initialSections: oldSections, finalSections: newSections)
99-
100-
switch self.decideViewTransition(self, tableView, differences) {
101-
case .animated:
102-
// each difference must be run in a separate 'performBatchUpdates', otherwise it crashes.
103-
// this is a limitation of Diff tool
104-
for difference in differences {
105-
let updateBlock = { [unowned self] in
106-
// sections must be set within updateBlock in 'performBatchUpdates'
107-
dataSource.setSections(difference.finalSections)
108-
tableView.batchUpdates(difference, animationConfiguration: self.animationConfiguration)
109-
}
110-
if #available(iOS 11, tvOS 11, *) {
111-
tableView.performBatchUpdates(updateBlock, completion: nil)
112-
} else {
113-
tableView.beginUpdates()
114-
updateBlock()
115-
tableView.endUpdates()
116-
}
89+
// if view is not in view hierarchy, performing batch updates will crash the app
90+
if tableView.window == nil {
91+
dataSource.setSections(newSections)
92+
tableView.reloadData()
93+
return
94+
}
95+
let oldSections = dataSource.sectionModels
96+
do {
97+
let differences = try Diff.differencesForSectionedView(initialSections: oldSections, finalSections: newSections)
98+
99+
switch dataSource.decideViewTransition(dataSource, tableView, differences) {
100+
case .animated:
101+
// each difference must be run in a separate 'performBatchUpdates', otherwise it crashes.
102+
// this is a limitation of Diff tool
103+
for difference in differences {
104+
let updateBlock = {
105+
// sections must be set within updateBlock in 'performBatchUpdates'
106+
dataSource.setSections(difference.finalSections)
107+
tableView.batchUpdates(difference, animationConfiguration: dataSource.animationConfiguration)
108+
}
109+
if #available(iOS 11, tvOS 11, *) {
110+
tableView.performBatchUpdates(updateBlock, completion: nil)
111+
} else {
112+
tableView.beginUpdates()
113+
updateBlock()
114+
tableView.endUpdates()
117115
}
118-
119-
case .reload:
120-
self.setSections(newSections)
121-
tableView.reloadData()
122-
return
123116
}
124-
}
125-
catch let e {
126-
rxDebugFatalError(e)
127-
self.setSections(newSections)
117+
118+
case .reload:
119+
dataSource.setSections(newSections)
128120
tableView.reloadData()
121+
return
129122
}
130123
}
124+
catch let e {
125+
rxDebugFatalError(e)
126+
dataSource.setSections(newSections)
127+
tableView.reloadData()
128+
}
131129
}
132130
}.on(observedEvent)
133131
}

Sources/RxDataSources/RxTableViewSectionedReloadDataSource.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ open class RxTableViewSectionedReloadDataSource<S: SectionModelType>
2323
open func tableView(_ tableView: UITableView, observedEvent: Event<Element>) {
2424
Binder(self) { dataSource, element in
2525
#if DEBUG
26-
self._dataSourceBound = true
26+
dataSource._dataSourceBound = true
2727
#endif
2828
dataSource.setSections(element)
2929
tableView.reloadData()

Sources/RxDataSources/UI+SectionedViewType.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,6 @@ public protocol SectionedViewType {
102102
func deleteSections(_ sections: [Int], animationStyle: UITableViewRowAnimation)
103103
func moveSection(_ from: Int, to: Int)
104104
func reloadSections(_ sections: [Int], animationStyle: UITableViewRowAnimation)
105-
106-
func batchUpdates<S>(_ changes: Changeset<S>, animationConfiguration: AnimationConfiguration)
107105
}
108106

109107
extension SectionedViewType {

0 commit comments

Comments
 (0)