fix: user timeline duplicate snapshot update issue

This commit is contained in:
CMK 2021-06-24 10:27:41 +08:00
parent ab19323bcd
commit 0366c83666
1 changed files with 8 additions and 8 deletions

View File

@ -62,10 +62,10 @@ final class UserTimelineViewModel {
.store(in: &disposeBag) .store(in: &disposeBag)
Publishers.CombineLatest4( Publishers.CombineLatest4(
statusFetchedResultsController.objectIDs.eraseToAnyPublisher(), statusFetchedResultsController.objectIDs.removeDuplicates(),
isBlocking.eraseToAnyPublisher(), isBlocking,
isBlockedBy.eraseToAnyPublisher(), isBlockedBy,
isSuspended.eraseToAnyPublisher() isSuspended
) )
.receive(on: DispatchQueue.main) .receive(on: DispatchQueue.main)
.debounce(for: .milliseconds(300), scheduler: DispatchQueue.main) .debounce(for: .milliseconds(300), scheduler: DispatchQueue.main)
@ -76,12 +76,12 @@ final class UserTimelineViewModel {
var items: [Item] = [] var items: [Item] = []
var snapshot = NSDiffableDataSourceSnapshot<StatusSection, Item>() var snapshot = NSDiffableDataSourceSnapshot<StatusSection, Item>()
snapshot.appendSections([.main]) snapshot.appendSections([.main])
defer { defer {
// not animate when empty items fix loader first appear layout issue // not animate when empty items fix loader first appear layout issue
diffableDataSource.apply(snapshot, animatingDifferences: !items.isEmpty) diffableDataSource.apply(snapshot, animatingDifferences: !items.isEmpty)
} }
guard !isBlocking else { guard !isBlocking else {
snapshot.appendItems([Item.emptyStateHeader(attribute: Item.EmptyStateHeaderAttribute(reason: .blocking))], toSection: .main) snapshot.appendItems([Item.emptyStateHeader(attribute: Item.EmptyStateHeaderAttribute(reason: .blocking))], toSection: .main)
return return
@ -97,14 +97,14 @@ final class UserTimelineViewModel {
snapshot.appendItems([Item.emptyStateHeader(attribute: Item.EmptyStateHeaderAttribute(reason: .suspended(name: name)))], toSection: .main) snapshot.appendItems([Item.emptyStateHeader(attribute: Item.EmptyStateHeaderAttribute(reason: .suspended(name: name)))], toSection: .main)
return return
} }
var oldSnapshotAttributeDict: [NSManagedObjectID : Item.StatusAttribute] = [:] var oldSnapshotAttributeDict: [NSManagedObjectID : Item.StatusAttribute] = [:]
let oldSnapshot = diffableDataSource.snapshot() let oldSnapshot = diffableDataSource.snapshot()
for item in oldSnapshot.itemIdentifiers { for item in oldSnapshot.itemIdentifiers {
guard case let .status(objectID, attribute) = item else { continue } guard case let .status(objectID, attribute) = item else { continue }
oldSnapshotAttributeDict[objectID] = attribute oldSnapshotAttributeDict[objectID] = attribute
} }
for objectID in objectIDs { for objectID in objectIDs {
let attribute = oldSnapshotAttributeDict[objectID] ?? Item.StatusAttribute() let attribute = oldSnapshotAttributeDict[objectID] ?? Item.StatusAttribute()
items.append(.status(objectID: objectID, attribute: attribute)) items.append(.status(objectID: objectID, attribute: attribute))