ribesg
03/25/2019, 10:14 AMsequence { }
gildor
03/25/2019, 10:16 AMribesg
03/25/2019, 10:16 AMgildor
03/25/2019, 10:17 AMribesg
03/25/2019, 10:26 AMgildor
03/25/2019, 10:27 AMsequence { }
use CoroutineScope.produce { }
ribesg
03/25/2019, 10:29 AMsend
would not be called until there’s a consumer, right?gildor
03/25/2019, 10:29 AMFredrik Larsen
03/25/2019, 12:43 PMgildor
03/25/2019, 1:34 PMFredrik Larsen
03/25/2019, 1:34 PMribesg
03/25/2019, 2:15 PMreceive
when I call send
, but when I call send
I already did the next request. Is there a way to start the next request when receive
is called, and not before, to prevent downloading potentially useless data?bdawg.io
03/25/2019, 3:03 PMonSend
would get you that. @gildor do you happen to know if onSend
is only a listener event or if it actually puts an item on the queue as well?Is there a way to start the next request whenis called, and not beforereceive
fun CoroutineScope.loadPages() = produce<List<MyItem>> {
var currentPage = 1
while (true) {
val page = loadPageNum(currentPage)
if (page.isEmpty()) {
break;
}
// await until item is received
send(page)
currentPage += 1
}
}
ribesg
03/25/2019, 3:12 PMbdawg.io
03/25/2019, 3:13 PMonSend
haha so I wasn't sure if it'd actually be useful or not lolribesg
03/25/2019, 3:16 PMbdawg.io
03/25/2019, 3:21 PMLazy<ReceiveChannel<YourType>>
and then have your consumer unwrap the value before consuming
val pages by lazy { loadPages() }
pages.consumeEach { ... }
ribesg
03/25/2019, 3:25 PMbdawg.io
03/25/2019, 3:46 PMLazy
(for above, not until pages.consumeEach
is called)?ribesg
03/26/2019, 9:11 AMsend
which is the blocking point. So you always have a unused page in memory, which may never be used. Your lazy
trick only works on the first page.gildor
03/26/2019, 9:11 AMribesg
03/26/2019, 9:13 AMinterface PageIterator<T> {
suspend fun next(): List<T>
}
object : PageIterator<MyType> {
private var lastReturnedPage = -1
private var done = false
override suspend fun next(): List<MyType> {
if (done) return emptyList()
val res = doMyHttpRequest(++lastReturnedPage)
done = lastReturnedPage == res.nbPages - 1
return res.results
}
}
gildor
03/26/2019, 9:13 AMribesg
03/26/2019, 9:15 AMgildor
03/26/2019, 9:16 AMribesg
03/26/2019, 9:18 AMgildor
03/26/2019, 9:21 AMribesg
03/26/2019, 9:23 AMsuspend
next
function doing an Http request, so I did just that. Hopefully I’ll be able to use existing things in the future.