Skip to content

Commit b581a3e

Browse files
committed
Add completion block to apply function
1 parent 36dd382 commit b581a3e

File tree

4 files changed

+24
-8
lines changed

4 files changed

+24
-8
lines changed

Sources/AppKit/CocoaCollectionViewDiffableDataSource.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,18 @@ open class CocoaCollectionViewDiffableDataSource<SectionIdentifierType: Hashable
3333
/// - snapshot: A snapshot object to be applied to data model.
3434
/// - animatingDifferences: A Boolean value indicating whether to update with
3535
/// diffing animation.
36+
/// - completion: An optional completion block which is called when the complete
37+
/// performing updates.
3638
public func apply(_ snapshot: DiffableDataSourceSnapshot<SectionIdentifierType, ItemIdentifierType>, animatingDifferences: Bool = true) {
3739
core.apply(
3840
snapshot,
3941
view: collectionView,
4042
animatingDifferences: animatingDifferences,
4143
performUpdates: { collectionView, changeset, setSections in
4244
collectionView.reload(using: changeset, setData: setSections)
43-
})
45+
},
46+
completion: completion
47+
)
4448
}
4549

4650
/// Returns a new snapshot object of current state.

Sources/Internal/DiffableDataSourceCore.swift

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ final class DiffableDataSourceCore<SectionIdentifierType: Hashable, ItemIdentifi
1313
_ snapshot: DiffableDataSourceSnapshot<SectionIdentifierType, ItemIdentifierType>,
1414
view: View?,
1515
animatingDifferences: Bool,
16-
performUpdates: @escaping (View, StagedChangeset<[Section]>, @escaping ([Section]) -> Void) -> Void
16+
performUpdates: @escaping (View, StagedChangeset<[Section]>, @escaping ([Section]) -> Void) -> Void,
17+
completion: (() -> Void)?
1718
) {
1819
dispatcher.dispatch { [weak self] in
1920
guard let self = self else {
@@ -35,15 +36,18 @@ final class DiffableDataSourceCore<SectionIdentifierType: Hashable, ItemIdentifi
3536
}
3637
}
3738

39+
CATransaction.begin()
40+
CATransaction.setCompletionBlock(completion)
41+
3842
if animatingDifferences {
3943
performDiffingUpdates()
4044
}
4145
else {
42-
CATransaction.begin()
4346
CATransaction.setDisableActions(true)
4447
performDiffingUpdates()
45-
CATransaction.commit()
4648
}
49+
50+
CATransaction.commit()
4751
}
4852
}
4953

Sources/UIKit/CollectionViewDiffableDataSource.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,18 @@ open class CollectionViewDiffableDataSource<SectionIdentifierType: Hashable, Ite
3939
/// - snapshot: A snapshot object to be applied to data model.
4040
/// - animatingDifferences: A Boolean value indicating whether to update with
4141
/// diffing animation.
42-
public func apply(_ snapshot: DiffableDataSourceSnapshot<SectionIdentifierType, ItemIdentifierType>, animatingDifferences: Bool = true) {
42+
/// - completion: An optional completion block which is called when the complete
43+
/// performing updates.
44+
public func apply(_ snapshot: DiffableDataSourceSnapshot<SectionIdentifierType, ItemIdentifierType>, animatingDifferences: Bool = true, completion: (() -> Void)? = nil) {
4345
core.apply(
4446
snapshot,
4547
view: collectionView,
4648
animatingDifferences: animatingDifferences,
4749
performUpdates: { collectionView, changeset, setSections in
4850
collectionView.reload(using: changeset, setData: setSections)
49-
})
51+
},
52+
completion: completion
53+
)
5054
}
5155

5256
/// Returns a new snapshot object of current state.

Sources/UIKit/TableViewDiffableDataSource.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,18 @@ open class TableViewDiffableDataSource<SectionIdentifierType: Hashable, ItemIden
3636
/// - snapshot: A snapshot object to be applied to data model.
3737
/// - animatingDifferences: A Boolean value indicating whether to update with
3838
/// diffing animation.
39-
public func apply(_ snapshot: DiffableDataSourceSnapshot<SectionIdentifierType, ItemIdentifierType>, animatingDifferences: Bool = true) {
39+
/// - completion: An optional completion block which is called when the complete
40+
/// performing updates.
41+
public func apply(_ snapshot: DiffableDataSourceSnapshot<SectionIdentifierType, ItemIdentifierType>, animatingDifferences: Bool = true, completion: (() -> Void)? = nil) {
4042
core.apply(
4143
snapshot,
4244
view: tableView,
4345
animatingDifferences: animatingDifferences,
4446
performUpdates: { tableView, changeset, setSections in
4547
tableView.reload(using: changeset, with: self.defaultRowAnimation, setData: setSections)
46-
})
48+
},
49+
completion: completion
50+
)
4751
}
4852

4953
/// Returns a new snapshot object of current state.

0 commit comments

Comments
 (0)