Skip to content

Commit 1d6674c

Browse files
authored
Merge pull request #12 from ra1028/completion-block
Add completion block
2 parents 36dd382 + 0fb3bbd commit 1d6674c

25 files changed

+523
-163
lines changed

DiffableDataSources.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Pod::Spec.new do |spec|
22
spec.name = 'DiffableDataSources'
3-
spec.version = '0.2.0'
3+
spec.version = '0.3.0'
44
spec.author = { 'ra1028' => '[email protected]' }
55
spec.homepage = 'https://github.com/ra1028/DiffableDataSources'
66
spec.documentation_url = 'https://ra1028.github.io/DiffableDataSources'

Gemfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
source "https://rubygems.org"
22

3-
gem 'cocoapods', '1.7.5'
4-
gem 'jazzy', '0.9.4'
3+
gem 'cocoapods', '1.8.4'
4+
gem 'jazzy', '0.11.2'

Gemfile.lock

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,27 @@
11
GEM
22
remote: https://rubygems.org/
33
specs:
4-
CFPropertyList (3.0.1)
4+
CFPropertyList (3.0.2)
55
activesupport (4.2.11.1)
66
i18n (~> 0.7)
77
minitest (~> 5.1)
88
thread_safe (~> 0.3, >= 0.3.4)
99
tzinfo (~> 1.1)
10+
algoliasearch (1.27.1)
11+
httpclient (~> 2.8, >= 2.8.3)
12+
json (>= 1.5.1)
1013
atomos (0.1.3)
1114
claide (1.0.3)
12-
cocoapods (1.7.5)
15+
cocoapods (1.8.4)
1316
activesupport (>= 4.0.2, < 5)
1417
claide (>= 1.0.2, < 2.0)
15-
cocoapods-core (= 1.7.5)
18+
cocoapods-core (= 1.8.4)
1619
cocoapods-deintegrate (>= 1.0.3, < 2.0)
1720
cocoapods-downloader (>= 1.2.2, < 2.0)
1821
cocoapods-plugins (>= 1.0.0, < 2.0)
1922
cocoapods-search (>= 1.0.0, < 2.0)
2023
cocoapods-stats (>= 1.0.0, < 2.0)
21-
cocoapods-trunk (>= 1.3.1, < 2.0)
24+
cocoapods-trunk (>= 1.4.0, < 2.0)
2225
cocoapods-try (>= 1.1.0, < 2.0)
2326
colored2 (~> 3.1)
2427
escape (~> 0.0.4)
@@ -27,65 +30,63 @@ GEM
2730
molinillo (~> 0.6.6)
2831
nap (~> 1.0)
2932
ruby-macho (~> 1.4)
30-
xcodeproj (>= 1.10.0, < 2.0)
31-
cocoapods-core (1.7.5)
33+
xcodeproj (>= 1.11.1, < 2.0)
34+
cocoapods-core (1.8.4)
3235
activesupport (>= 4.0.2, < 6)
36+
algoliasearch (~> 1.0)
37+
concurrent-ruby (~> 1.1)
3338
fuzzy_match (~> 2.0.4)
3439
nap (~> 1.0)
3540
cocoapods-deintegrate (1.0.4)
36-
cocoapods-downloader (1.2.2)
41+
cocoapods-downloader (1.3.0)
3742
cocoapods-plugins (1.0.0)
3843
nap
3944
cocoapods-search (1.0.0)
4045
cocoapods-stats (1.1.0)
41-
cocoapods-trunk (1.4.0)
46+
cocoapods-trunk (1.4.1)
4247
nap (>= 0.8, < 2.0)
4348
netrc (~> 0.11)
4449
cocoapods-try (1.1.0)
4550
colored2 (3.1.2)
4651
concurrent-ruby (1.1.5)
4752
escape (0.0.4)
48-
ffi (1.11.1)
53+
ffi (1.11.3)
4954
fourflusher (2.3.1)
5055
fuzzy_match (2.0.4)
5156
gh_inspector (1.1.3)
57+
httpclient (2.8.3)
5258
i18n (0.9.5)
5359
concurrent-ruby (~> 1.0)
54-
jazzy (0.9.4)
55-
cocoapods (~> 1.0)
56-
mustache (~> 0.99)
60+
jazzy (0.11.2)
61+
cocoapods (~> 1.5)
62+
mustache (~> 1.1)
5763
open4
58-
redcarpet (~> 3.2)
64+
redcarpet (~> 3.4)
5965
rouge (>= 2.0.6, < 4.0)
60-
sass (~> 3.4)
66+
sassc (~> 2.1)
6167
sqlite3 (~> 1.3)
6268
xcinvoke (~> 0.3.0)
69+
json (2.2.0)
6370
liferaft (0.0.6)
64-
minitest (5.11.3)
71+
minitest (5.13.0)
6572
molinillo (0.6.6)
66-
mustache (0.99.8)
73+
mustache (1.1.1)
6774
nanaimo (0.2.6)
6875
nap (1.1.0)
6976
netrc (0.11.0)
7077
open4 (1.3.4)
71-
rb-fsevent (0.10.3)
72-
rb-inotify (0.10.0)
73-
ffi (~> 1.0)
74-
redcarpet (3.4.0)
75-
rouge (3.4.1)
78+
redcarpet (3.5.0)
79+
rouge (3.13.0)
7680
ruby-macho (1.4.0)
77-
sass (3.7.4)
78-
sass-listen (~> 4.0.0)
79-
sass-listen (4.0.0)
80-
rb-fsevent (~> 0.9, >= 0.9.4)
81-
rb-inotify (~> 0.9, >= 0.9.7)
81+
sassc (2.2.1)
82+
ffi (~> 1.9)
8283
sqlite3 (1.4.1)
8384
thread_safe (0.3.6)
8485
tzinfo (1.2.5)
8586
thread_safe (~> 0.1)
8687
xcinvoke (0.3.0)
8788
liferaft (~> 0.0.6)
88-
xcodeproj (1.12.0)
89+
xcodeproj (1.13.0)
8990
CFPropertyList (>= 2.3.3, < 4.0)
9091
atomos (~> 0.1.3)
9192
claide (>= 1.0.2, < 2.0)
@@ -96,8 +97,8 @@ PLATFORMS
9697
ruby
9798

9899
DEPENDENCIES
99-
cocoapods (= 1.7.5)
100-
jazzy (= 0.9.4)
100+
cocoapods (= 1.8.4)
101+
jazzy (= 0.11.2)
101102

102103
BUNDLED WITH
103-
2.0.1
104+
2.0.2

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,9 @@ let snapshot = DiffableDataSourceSnapshot<Section, User>()
144144
snapshot.appendSections([.main])
145145
snapshot.appendItems(users)
146146

147-
dataSource.apply(snapshot)
147+
dataSource.apply(snapshot) {
148+
// completion
149+
}
148150
```
149151

150152
Check the documentation for more detailed API.

Sources/AppKit/CocoaCollectionViewDiffableDataSource.swift

Lines changed: 6 additions & 2 deletions
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-
public func apply(_ snapshot: DiffableDataSourceSnapshot<SectionIdentifierType, ItemIdentifierType>, animatingDifferences: Bool = true) {
36+
/// - completion: An optional completion block which is called when the complete
37+
/// performing updates.
38+
public func apply(_ snapshot: DiffableDataSourceSnapshot<SectionIdentifierType, ItemIdentifierType>, animatingDifferences: Bool = true, completion: (() -> Void)? = nil) {
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.

Tests/CocoaCollectionViewDiffableDataSourceTests.swift

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,29 @@ final class CocoaCollectionViewDiffableDataSourceTests: XCTestCase {
2222

2323
var snapshot = DiffableDataSourceSnapshot<Int, Int>()
2424

25-
dataSource.apply(snapshot)
25+
let e1 = expectation(description: "testApply() e1")
26+
dataSource.apply(snapshot, completion: e1.fulfill)
27+
wait(for: [e1], timeout: 1)
2628
XCTAssertEqual(collectionView.isPerformBatchUpdatesCalledCount, 0)
2729

2830
snapshot.appendSections([0])
2931
snapshot.appendItems([0])
3032

31-
dataSource.apply(snapshot)
33+
let e2 = expectation(description: "testApply() e2")
34+
dataSource.apply(snapshot, completion: e2.fulfill)
35+
wait(for: [e2], timeout: 1)
3236
XCTAssertEqual(collectionView.isPerformBatchUpdatesCalledCount, 1)
3337

34-
dataSource.apply(snapshot)
38+
let e3 = expectation(description: "testApply() e3")
39+
dataSource.apply(snapshot, completion: e3.fulfill)
40+
wait(for: [e3], timeout: 1)
3541
XCTAssertEqual(collectionView.isPerformBatchUpdatesCalledCount, 1)
3642

3743
snapshot.appendItems([1])
3844

39-
dataSource.apply(snapshot)
45+
let e4 = expectation(description: "testApply() e4")
46+
dataSource.apply(snapshot, completion: e4.fulfill)
47+
wait(for: [e4], timeout: 1)
4048
XCTAssertEqual(collectionView.isPerformBatchUpdatesCalledCount, 2)
4149
}
4250

Tests/CollectionViewDiffableDataSourceTests.swift

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,29 @@ final class CollectionViewDiffableDataSourceTests: XCTestCase {
2222

2323
var snapshot = DiffableDataSourceSnapshot<Int, Int>()
2424

25-
dataSource.apply(snapshot)
25+
let e1 = expectation(description: "testApply() e1")
26+
dataSource.apply(snapshot, completion: e1.fulfill)
27+
wait(for: [e1], timeout: 1)
2628
XCTAssertEqual(collectionView.isPerformBatchUpdatesCalledCount, 0)
2729

2830
snapshot.appendSections([0])
2931
snapshot.appendItems([0])
3032

31-
dataSource.apply(snapshot)
33+
let e2 = expectation(description: "testApply() e2")
34+
dataSource.apply(snapshot, completion: e2.fulfill)
35+
wait(for: [e2], timeout: 1)
3236
XCTAssertEqual(collectionView.isPerformBatchUpdatesCalledCount, 1)
3337

34-
dataSource.apply(snapshot)
38+
let e3 = expectation(description: "testApply() e3")
39+
dataSource.apply(snapshot, completion: e3.fulfill)
40+
wait(for: [e3], timeout: 1)
3541
XCTAssertEqual(collectionView.isPerformBatchUpdatesCalledCount, 1)
3642

3743
snapshot.appendItems([1])
3844

39-
dataSource.apply(snapshot)
45+
let e4 = expectation(description: "testApply() e4")
46+
dataSource.apply(snapshot, completion: e4.fulfill)
47+
wait(for: [e4], timeout: 1)
4048
XCTAssertEqual(collectionView.isPerformBatchUpdatesCalledCount, 2)
4149
}
4250

0 commit comments

Comments
 (0)