Skip to content

Commit ef88708

Browse files
committed
add async version of update() method + update examples
1 parent b1cfba2 commit ef88708

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

Example/Sources/List/ListViewController.swift

+6-3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ final class ListViewController: ExampleViewController, CellEventCoordinator {
1919

2020
lazy var driver = CollectionViewDriver(
2121
view: self.collectionView,
22+
options: .init(diffOnBackgroundQueue: true),
2223
emptyViewProvider: sharedEmptyViewProvider,
2324
cellEventCoordinator: self
2425
)
@@ -27,9 +28,11 @@ final class ListViewController: ExampleViewController, CellEventCoordinator {
2728
didSet {
2829
// Every time the model updates, regenerate and set the view model
2930
let viewModel = self.makeViewModel()
30-
self.driver.update(viewModel: viewModel, animated: true) {
31-
print("list did update!")
32-
print($0.viewModel)
31+
32+
Task { @MainActor in
33+
await self.driver.update(viewModel: viewModel)
34+
print("list did update! async")
35+
print(viewModel)
3336
}
3437
}
3538
}

Sources/CollectionViewDriver.swift

+19
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,25 @@ public final class CollectionViewDriver: NSObject {
147147
)
148148
}
149149

150+
/// An async version of ``update(viewModel:animated:completion:)``.
151+
///
152+
/// Updates the collection with the provided `viewModel`.
153+
/// This method will trigger a diff between the previous view model and the newly provided view model.
154+
///
155+
/// - Parameters:
156+
/// - viewModel: The new collection view model.
157+
/// - animated: Whether or not to animate updates.
158+
///
159+
/// - Warning: If you provide a `viewModel` with an `id` different from the previous one,
160+
/// this is considered a *replacement*. By default, the driver will animate the diff between the view models.
161+
/// You can customize this behavior via the ``options`` for the driver.
162+
public func update(viewModel new: CollectionViewModel, animated: Bool = true) async {
163+
await withCheckedContinuation { continuation in
164+
self.update(viewModel: new, animated: animated)
165+
continuation.resume()
166+
}
167+
}
168+
150169
// MARK: Private
151170

152171
private func _registerAllViews(for viewModel: CollectionViewModel) {

0 commit comments

Comments
 (0)