@@ -15,11 +15,13 @@ public extension UITableView {
15
15
/// updates should be stopped and performed reloadData. Default is nil.
16
16
/// - setData: A closure that takes the collection as a parameter.
17
17
/// The collection should be set to data-source of UITableView.
18
+ /// - completion: A closure that is called when the reload is completed.
18
19
func reload< C> (
19
20
using stagedChangeset: StagedChangeset < C > ,
20
21
with animation: @autoclosure ( ) -> RowAnimation ,
21
22
interrupt: ( ( Changeset < C > ) -> Bool ) ? = nil ,
22
- setData: ( C ) -> Void
23
+ setData: ( C ) -> Void ,
24
+ completion: ( ( ) -> Void ) ? = nil
23
25
) {
24
26
reload (
25
27
using: stagedChangeset,
@@ -30,7 +32,8 @@ public extension UITableView {
30
32
insertRowsAnimation: animation ( ) ,
31
33
reloadRowsAnimation: animation ( ) ,
32
34
interrupt: interrupt,
33
- setData: setData
35
+ setData: setData,
36
+ completion: completion
34
37
)
35
38
}
36
39
@@ -52,6 +55,7 @@ public extension UITableView {
52
55
/// updates should be stopped and performed reloadData. Default is nil.
53
56
/// - setData: A closure that takes the collection as a parameter.
54
57
/// The collection should be set to data-source of UITableView.
58
+ /// - completion: A closure that is called when the reload is completed.
55
59
func reload< C> (
56
60
using stagedChangeset: StagedChangeset < C > ,
57
61
deleteSectionsAnimation: @autoclosure ( ) -> RowAnimation ,
@@ -61,8 +65,14 @@ public extension UITableView {
61
65
insertRowsAnimation: @autoclosure ( ) -> RowAnimation ,
62
66
reloadRowsAnimation: @autoclosure ( ) -> RowAnimation ,
63
67
interrupt: ( ( Changeset < C > ) -> Bool ) ? = nil ,
64
- setData: ( C ) -> Void
68
+ setData: ( C ) -> Void ,
69
+ completion: ( ( ) -> Void ) ? = nil
65
70
) {
71
+ let group = DispatchGroup ( )
72
+ defer {
73
+ group. notify ( queue: . main) { completion ? ( ) }
74
+ }
75
+
66
76
if case . none = window, let data = stagedChangeset. last? . data {
67
77
setData ( data)
68
78
return reloadData ( )
@@ -74,7 +84,8 @@ public extension UITableView {
74
84
return reloadData ( )
75
85
}
76
86
77
- _performBatchUpdates {
87
+ group. enter ( )
88
+ _performBatchUpdates ( {
78
89
setData ( changeset. data)
79
90
80
91
if !changeset. sectionDeleted. isEmpty {
@@ -108,18 +119,21 @@ public extension UITableView {
108
119
for (source, target) in changeset. elementMoved {
109
120
moveRow ( at: IndexPath ( row: source. element, section: source. section) , to: IndexPath ( row: target. element, section: target. section) )
110
121
}
111
- }
122
+ } , completion : { group . leave ( ) } )
112
123
}
113
124
}
114
125
115
- private func _performBatchUpdates( _ updates: ( ) -> Void ) {
126
+ private func _performBatchUpdates( _ updates: ( ) -> Void , completion : ( ( ) -> Void ) ? ) {
116
127
if #available( iOS 11 . 0 , tvOS 11 . 0 , * ) {
117
- performBatchUpdates ( updates)
128
+ performBatchUpdates ( updates, completion : { _ in completion ? ( ) } )
118
129
}
119
130
else {
131
+ CATransaction . begin ( )
132
+ CATransaction . setCompletionBlock ( completion)
120
133
beginUpdates ( )
121
134
updates ( )
122
135
endUpdates ( )
136
+ CATransaction . commit ( )
123
137
}
124
138
}
125
139
}
@@ -137,11 +151,18 @@ public extension UICollectionView {
137
151
/// updates should be stopped and performed reloadData. Default is nil.
138
152
/// - setData: A closure that takes the collection as a parameter.
139
153
/// The collection should be set to data-source of UICollectionView.
154
+ /// - completion: A closure that is called when the reload is completed.
140
155
func reload< C> (
141
156
using stagedChangeset: StagedChangeset < C > ,
142
157
interrupt: ( ( Changeset < C > ) -> Bool ) ? = nil ,
143
- setData: ( C ) -> Void
158
+ setData: ( C ) -> Void ,
159
+ completion: ( ( ) -> Void ) ? = nil
144
160
) {
161
+ let group = DispatchGroup ( )
162
+ defer {
163
+ group. notify ( queue: . main) { completion ? ( ) }
164
+ }
165
+
145
166
if case . none = window, let data = stagedChangeset. last? . data {
146
167
setData ( data)
147
168
return reloadData ( )
@@ -153,6 +174,7 @@ public extension UICollectionView {
153
174
return reloadData ( )
154
175
}
155
176
177
+ group. enter ( )
156
178
performBatchUpdates ( {
157
179
setData ( changeset. data)
158
180
@@ -187,7 +209,7 @@ public extension UICollectionView {
187
209
for (source, target) in changeset. elementMoved {
188
210
moveItem ( at: IndexPath ( item: source. element, section: source. section) , to: IndexPath ( item: target. element, section: target. section) )
189
211
}
190
- } )
212
+ } , completion : { _ in group . leave ( ) } )
191
213
}
192
214
}
193
215
}
0 commit comments