fix: suggestion account not thread safe issue

This commit is contained in:
CMK 2021-07-07 17:09:12 +08:00
parent 54ea1e9c0d
commit cd36d7e4c3
1 changed files with 21 additions and 11 deletions

View File

@ -49,14 +49,22 @@ final class SuggestionAccountViewModel: NSObject {
super.init()
Publishers.CombineLatest(self.accounts,self.selectedAccounts)
Publishers.CombineLatest(
self.accounts,
self.selectedAccounts
)
.receive(on: RunLoop.main)
.sink { [weak self] accounts,selectedAccounts in
self?.applyTableViewDataSource(accounts: accounts)
self?.applySelectedCollectionViewDataSource(accounts: selectedAccounts)
}
.store(in: &disposeBag)
Publishers.CombineLatest(self.selectedAccounts,self.headerPlaceholderCount)
Publishers.CombineLatest(
self.selectedAccounts,
self.headerPlaceholderCount
)
.receive(on: RunLoop.main)
.sink { [weak self] selectedAccount,count in
self?.applySelectedCollectionViewDataSource(accounts: selectedAccount)
}
@ -133,6 +141,7 @@ final class SuggestionAccountViewModel: NSObject {
}
func applyTableViewDataSource(accounts: [NSManagedObjectID]) {
assert(Thread.isMainThread)
guard let dataSource = diffableDataSource else { return }
var snapshot = NSDiffableDataSourceSnapshot<RecommendAccountSection, NSManagedObjectID>()
snapshot.appendSections([.main])
@ -141,6 +150,7 @@ final class SuggestionAccountViewModel: NSObject {
}
func applySelectedCollectionViewDataSource(accounts: [NSManagedObjectID]) {
assert(Thread.isMainThread)
guard let count = headerPlaceholderCount.value else { return }
guard let dataSource = collectionDiffableDataSource else { return }
var snapshot = NSDiffableDataSourceSnapshot<SelectedAccountSection, SelectedAccountItem>()