Skip to content
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

Instant Search + Paging 3 + Flow #374

Open
GautierLouis opened this issue Nov 22, 2022 · 4 comments
Open

Instant Search + Paging 3 + Flow #374

GautierLouis opened this issue Nov 22, 2022 · 4 comments

Comments

@GautierLouis
Copy link

Describe the bug 🐛
I'm implementing Instant Search on my android app using Paging 3 and flow to update the UI
I followed this article : https://www.algolia.com/doc/api-reference/widgets/multi-hits/android/#examples
This is quite simple but, for some reason either flow or live data are not updated when a query is typed.

To Reproduce 🔍
My entire ViewModel ->
```
private val searcher = MultiSearcher(
applicationID = ApplicationID("-----"),
apiKey = APIKey("----")
)
private val pagingConfig = PagingConfig(pageSize = 50)
private val indexProblem = IndexName(INDEX_PROBLEM)
private val indexArea = IndexName(INDEX_AREA)
private val problemSearcher = searcher.addHitsSearcher(indexName = indexProblem)
private val areaSearcher = searcher.addHitsSearcher(indexName = indexArea)
private val filterState = FilterState()
private val connection = ConnectionHandler()
private val problemPaginator = Paginator(
problemSearcher,
pagingConfig,
transformer = { hit -> hit.deserialize(ProblemRemote.serializer()) }
)
private val areaPaginator = Paginator(
areaSearcher,
pagingConfig,
transformer = { hit -> hit.deserialize(AreaRemote.serializer()) }
)

init {
    connection += filterState.connectPaginator(problemPaginator)
    connection += filterState.connectPaginator(areaPaginator)
}

val problems
    get() = problemPaginator.flow

val areas
    get() = areaPaginator.flow

fun search(query: String? = "") {
    searcher.setQuery(query)
    searcher.searchAsync()
}

override fun onCleared() {
    super.onCleared()
    searcher.cancel()
    connection.disconnect()
}

}


And in the onCreate of an activity
    searchViewModel.problems.onEach {
        println("RESULT PROBLEM $it")
        problemAdapter.submitData(it)
    }
    searchViewModel.areas.onEach {
        println("RESULT AREAS $it")
        areaAdapter.submitData(it)
    }

**Expected behavior 💭**
I expect that my flow are called when a type a query 

**Environment:**
 - OS: Mac M1 
 - Library Version 3.1.4

**Additional context**
Transformer (in Paginator) is never called, it's not just a flow/livedata issue 
@aallam
Copy link
Member

aallam commented Nov 23, 2022

Hi @GautierLouis, that's because you are using onEach, which is an intermediate operator, your flow never starts.
From Flow documentation:

Intermediate operators on the flow such as map, filter, take, zip, etc are functions that are applied to the upstream flow or flows and return a downstream flow where further operators can be applied to. Intermediate operations do not execute any code in the flow and are not suspending functions themselves.

What you need is a terminal operator to trigger the flow to start listening for values, such as collect. The simplest way would using LifecycleScope:

lifecycleScope.launch {
    searchViewModel.problems.collect {
        println("RESULT PROBLEM $it")
        problemAdapter.submitData(it)
    }
    searchViewModel.areas.collect {
        println("RESULT AREAS $it")
        areaAdapter.submitData(it)
    }
}

@GautierLouis
Copy link
Author

Hi @aallam ,
Thanks. I did, I wasn't using flow correctly. But it still not working.
problemPaginator does his transformation but I only got 1 data collected in my flow
areaPaginator isn't called at all
When I enter a query, nothing change (no transformation)

@aallam
Copy link
Member

aallam commented Nov 24, 2022

Hi @GautierLouis, indeed paging3 and multisearch doesn't seem to work as expected, we have added this issue to our backlog.
I would suggest either using two HitsSearchers or don't use pagination like in this example, or this one for compose.

@GautierLouis
Copy link
Author

No worries, I've managed to do it with a basic subscriptions + flow, and I don't have any skills on compose yet.
Let me know when it will be fixed. thanks for your time :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants