I'm making a flow that produces a list of files fr...
# coroutines
d
I'm making a flow that produces a list of files from Google Cloud Storage through running paginated executions. The actual RPCs should be in Dispatchers.IO (I think!). I'm not sure if I'm supposed to use withContext for this or flowOn. What I have is:
Copy code
fun listFiles(bucket: String, prefix: String): Flow<Blob> = flow {
		var page = withContext(<http://Dispatchers.IO|Dispatchers.IO>) {
			gcs.list(bucket, BlobListOption.prefix(prefix))
		}
		while (true) {
			emit(page)
			if (!page.hasNextPage()) {
				break
			}
			page = withContext(<http://Dispatchers.IO|Dispatchers.IO>) { page.nextPage }
		}
	}.flatMapConcat { page ->
		page.values.asFlow()
	}
but I don't know if that is right
l
Use
flowOn
d
like this?
Copy code
flow {
		var page = gcs.list(bucket, BlobListOption.prefix(prefix))
		while (true) {
			emit(page)
			if (!page.hasNextPage()) {
				break
			}
			page = page.nextPage
		}
	}
		.flowOn(<http://Dispatchers.IO|Dispatchers.IO>)
		.flatMapConcat { page -> page.values.asFlow() }
l
Indent is not standard (should be 4 spaces). Looks correct to me, but I recommend you to read the docs of
flowOn
(and
Flow
) in general to understand what you do and get confidence.