-
-
Notifications
You must be signed in to change notification settings - Fork 292
Refactoring search for correctness and efficiency and clarity #5630
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
1ffff21 to
def020f
Compare
- dealing with race conditions b/c the code was structured weirdly - contacts view model fetches users on init for some reason, this is a work around - coroutines should always throw cancellation exception Signed-off-by: rapterjet2004 <juliuslinus1@gmail.com>
def020f to
7b3f24d
Compare
|
APK file: https://www.kaminsky.me/nc-dev/android-artifacts/5630.apk |
mahibi
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I approve as it solves the issue. 👍
But we should solve that in a different way in a follow up PR:
A better solution (but not for this PR) will be to move the fetching into the viewModel and there we should make use of combine to combine different data sources.
This should be done in general throughout the app and not only for this scenario.
Using async + awaitAll is not necessary then and also mutex handling is not needed.
So in the viewModel there should be code like:
viewModelScope.launch {
combine(
repository.fetchUsersFlow(filter),
repository.fetchOpenConversationsFlow(filter)
) { users, conversations ->
users + conversations
}.collect { combined ->
_searchResults.value = combined
}
}
So in general, move more logic to view model while keeping the UI "dumb". The UI should only get the ready to use data for displaying..
We should make use of combine in many places which will also reduce boilerplate and complex code as we use too many flows that emit to the UI's.
Using combine will avoid to have too many flows.
Having too many flows makes code hard to read and it's hard to keep track where something is catched and what happens.

The
getOpenConversationsAPI endpoint was being called every time the user gets a list of rooms, which was very inefficient, considering how often that occurs. This PR, moves that functionality to the querying logic, while also refactoring the querying logic for clarity and correctness.Now every time you search, it launches a Coroutine that gets the open conversations/users with structured concurrency, and processes their data in a thread safe way, before retrieving the messages async and adding them to the adapter when ready.
🚧 TODO
🏁 Checklist
/backport to stable-xx.x