Suppose I have a flow of search results, where eac...
# coroutines
m
Suppose I have a flow of search results, where each emitted item is a single search result item (i.e. the flow needs to be collected into a list which forms the search results). Is there a recommended pattern for how to control the collection of this flow from an Android recyclerview adapter? For example, so that items are collected as the user scrolls down the recyclerview perhaps prefetching 5-10 items ahead of the last visible item.
i
This seems like the wrong way of approaching the problem. Have you looked at Paging 3.0? It does exactly all of this. https://developer.android.com/topic/libraries/architecture/paging/v3-overview
m
I don’t think that fits my use case. The flow is rather complex in the way items are emitted, not some simple query. No way of knowing the size etc.
i
That isn't a requirement with Paging 3.0
The entire Data Source API is Flow based
m
Isn’t that a flow of list of all items, rather than what I have - a flow of items that make up the list? The examples I see here https://developer.android.com/reference/kotlin/androidx/paging/PagingSource show a page number or key being passed in, but I don’t see how that can work with my use case since I would need to calculate all the preceding items first to know the page number and I cannot work out item N from item N-1
i
There's no requirement to use page numbers or anything of the sort if that's not what you need. You get to pick what LoadParams you want, what they they are, literally everything.
m
Ok, so PagingSource has a key generic, so I guess you are saying that can be ignored (use a dummy value) since there is no such key I could use for my flow. But then wouldn’t that mean loading the entire list in one go? Do you have some kind of sample that describes this usage?
Maybe there is a way to convert the flow of search result items into something which is keyed/paged. At the moment I collect the flow to build a flow of a list of search items (i.e. a list gradually gets bigger until the underlying flow completes). But maybe this collector could associate a key/page with each incremental change. How does that sound?