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