I have UITableView with 2 separated sections of users.
1 section - online users
2 section - offline users
When I try to make two separated realmdb request like that:
conversationsResult = dbUsers.filter{$0.isOnline}
conversationsNotificationToken = conversationsResult?.observe { [weak output] changes in
guard let output = output else { return }
output.receivedConversationsDataChanges(changes)
}
For users who is online and for user who is offline
conversationsOfflineResult = dbUsers.filter{!$0.isOnline}
conversationsNotificationTokenOffline = conversationsResultOffline?.observe { [weak output] changes in
guard let output = output else { return }
output.receivedConversationsDataChangesOffline(changes)
}
Each of changes change only specific section by change example method from documentation:
func applyChanges<T>(changes: RealmCollectionChange<T>, section: Int = 0) {
switch changes {
case .initial:
reloadSections([section], with: .none)
case .update(_, let deletions, let insertions, let updates):
let fromRow = {(row: Int) in
return IndexPath(row: row, section: section)}
beginUpdates()
deleteRows(at: deletions.map(fromRow), with: .automatic)
insertRows(at: insertions.map(fromRow), with: .automatic)
reloadRows(at: updates.map(fromRow), with: .none)
endUpdates()
default: break
}
}
But when user change status from offline to online or vice verse both sections update async and my app crashed. Because data array is wrong.
Currently I use reloadData instead method for update whole talbeView. But its ugly solution.
Help find correct solution for update UITableView/UICollectionView with multiply section based on Results of Realm db class.