Skip to content

Commit 5141779

Browse files
committed
Add completion handlers to the AppKit extensions
1 parent 7b1b7cb commit 5141779

File tree

1 file changed

+27
-5
lines changed

1 file changed

+27
-5
lines changed

Sources/Extensions/AppKitExtension.swift

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,23 @@ public extension NSTableView {
1515
/// updates should be stopped and performed reloadData. Default is nil.
1616
/// - setData: A closure that takes the collection as a parameter.
1717
/// The collection should be set to data-source of NSTableView.
18+
/// - completion: A closure that is called when the reload is completed.
1819

1920
func reload<C>(
2021
using stagedChangeset: StagedChangeset<C>,
2122
with animation: @autoclosure () -> NSTableView.AnimationOptions,
2223
interrupt: ((Changeset<C>) -> Bool)? = nil,
23-
setData: (C) -> Void
24+
setData: (C) -> Void,
25+
completion: (() -> Void)? = nil
2426
) {
2527
reload(
2628
using: stagedChangeset,
2729
deleteRowsAnimation: animation(),
2830
insertRowsAnimation: animation(),
2931
reloadRowsAnimation: animation(),
3032
interrupt: interrupt,
31-
setData: setData
33+
setData: setData,
34+
completion: completion
3235
)
3336
}
3437

@@ -47,14 +50,21 @@ public extension NSTableView {
4750
/// updates should be stopped and performed reloadData. Default is nil.
4851
/// - setData: A closure that takes the collection as a parameter.
4952
/// The collection should be set to data-source of NSTableView.
53+
/// - completion: A closure that is called when the reload is completed.
5054
func reload<C>(
5155
using stagedChangeset: StagedChangeset<C>,
5256
deleteRowsAnimation: @autoclosure () -> NSTableView.AnimationOptions,
5357
insertRowsAnimation: @autoclosure () -> NSTableView.AnimationOptions,
5458
reloadRowsAnimation: @autoclosure () -> NSTableView.AnimationOptions,
5559
interrupt: ((Changeset<C>) -> Bool)? = nil,
56-
setData: (C) -> Void
60+
setData: (C) -> Void,
61+
completion: (() -> Void)? = nil
5762
) {
63+
let group = DispatchGroup()
64+
group.notify(queue: .main) { completion?() }
65+
group.enter()
66+
defer { group.leave() }
67+
5868
if case .none = window, let data = stagedChangeset.last?.data {
5969
setData(data)
6070
return reloadData()
@@ -66,6 +76,9 @@ public extension NSTableView {
6676
return reloadData()
6777
}
6878

79+
group.enter()
80+
CATransaction.begin()
81+
CATransaction.setCompletionBlock({ group.leave() })
6982
beginUpdates()
7083
setData(changeset.data)
7184

@@ -86,6 +99,7 @@ public extension NSTableView {
8699
}
87100

88101
endUpdates()
102+
CATransaction.commit()
89103
}
90104
}
91105
}
@@ -104,11 +118,18 @@ public extension NSCollectionView {
104118
/// updates should be stopped and performed reloadData. Default is nil.
105119
/// - setData: A closure that takes the collection as a parameter.
106120
/// The collection should be set to data-source of NSCollectionView.
121+
/// - completion: A closure that is called when the reload is completed.
107122
func reload<C>(
108123
using stagedChangeset: StagedChangeset<C>,
109124
interrupt: ((Changeset<C>) -> Bool)? = nil,
110-
setData: (C) -> Void
125+
setData: (C) -> Void,
126+
completion: (() -> Void)? = nil
111127
) {
128+
let group = DispatchGroup()
129+
group.notify(queue: .main) { completion?() }
130+
group.enter()
131+
defer { group.leave() }
132+
112133
if case .none = window, let data = stagedChangeset.last?.data {
113134
setData(data)
114135
return reloadData()
@@ -120,6 +141,7 @@ public extension NSCollectionView {
120141
return reloadData()
121142
}
122143

144+
group.enter()
123145
animator().performBatchUpdates({
124146
setData(changeset.data)
125147

@@ -138,7 +160,7 @@ public extension NSCollectionView {
138160
for (source, target) in changeset.elementMoved {
139161
moveItem(at: IndexPath(item: source.element, section: source.section), to: IndexPath(item: target.element, section: target.section))
140162
}
141-
})
163+
}, completionHandler: { _ in group.leave() })
142164
}
143165
}
144166
}

0 commit comments

Comments
 (0)