Skip to content

Instantly share code, notes, and snippets.

@colinhumber
Created January 6, 2022 21:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save colinhumber/9dc79cd071e2f21f304c81273b0daa35 to your computer and use it in GitHub Desktop.
Save colinhumber/9dc79cd071e2f21f304c81273b0daa35 to your computer and use it in GitHub Desktop.
// works fine, but receiveValue may return on different threads
viewModel.$snapshot
.sink { [weak self] snapshot in
guard let self = self else { return }
var animateDifferences = false
switch self.viewModel.state {
case .idle, .empty, .loading, .searching:
animateDifferences = false
case .loaded:
animateDifferences = true
}
self.tableViewDataSource.apply(snapshot, animatingDifferences: animateDifferences)
}
.store(in: &cancellables)
// always calls apply on main thread, but table view gets laid out in an animation, expanding from (0, 0) origin to full width/height
viewModel.$snapshot
.receive(on: DispatchQueue.main)
.sink { [weak self] snapshot in
guard let self = self else { return }
var animateDifferences = false
switch self.viewModel.state {
case .idle, .empty, .loading, .searching:
animateDifferences = false
case .loaded:
animateDifferences = true
}
self.tableViewDataSource.apply(snapshot, animatingDifferences: animateDifferences)
}
.store(in: &cancellables)
// works as expected with no table view layout animation, but required additional dispatch which feels gross, but always runs apply on the main thread
viewModel.$snapshot
.sink { [weak self] snapshot in
guard let self = self else { return }
var animateDifferences = false
switch self.viewModel.state {
case .idle, .empty, .loading, .searching:
animateDifferences = false
case .loaded:
animateDifferences = true
}
DispatchQueue.main.async {
self.tableViewDataSource.apply(snapshot, animatingDifferences: animateDifferences)
}
}
.store(in: &cancellables)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment