ursus
09/30/2019, 5:59 AMPablichjenkov
09/30/2019, 7:40 AMPablichjenkov
09/30/2019, 7:55 AMursus
09/30/2019, 4:27 PMPablichjenkov
09/30/2019, 5:16 PMflowOn
to specify a custom CoroutineContext
where you want the Flows to execute on. Setup this CoroutineContext
with a custom Dispatcher
that has a max of X threads. Check the Default Dispatcher or asCoroutineDispatcher
to create one from an ExecutorService if the Default don't work for you.
With Actor:
Actors will give you more grained control since you can "manually" handle your request. You can also optimize for Inflight Requests
to a same resource avoiding n
parallel calls to the same remote resource.
- This solution needs more code. An Actor is an interesting Abstract concept but in practicality is a Queue receiving events and processing them in sequence. That you can achieve wrapping a Channel. Then to handle the event stream use channel.consumeEach{... your lambda ...}
You can still use actorOf
builder present in the framework but don't know if you are going to like the Experimental/Obsolete tag that has on it.
For the first approach to mimic an Actor check this:
https://github.com/pablichjenkov/Android-Actor/blob/master/app/src/main/java/com/hamperapp/actor/Actor.ktursus
09/30/2019, 5:48 PMursus
09/30/2019, 5:49 PMPablichjenkov
09/30/2019, 6:48 PMursus
09/30/2019, 8:06 PMursus
09/30/2019, 8:07 PMursus
09/30/2019, 8:07 PMPablichjenkov
09/30/2019, 8:23 PMflatMapMerge
, I have no idea what that param is for.
By Request Control Logic I mean putting code in your Remote Data Manager class to limit the amount of ongoing request. A BlockingQueue will be needed to put incoming exceeded requests on hold while the current ones complete. There is probably something like that already done but less likely in Kotlin.
If you are using ok-http you can setup its internal executor. Again Executors are well optimized to minimize resource consumption when there is no work to do. So having a couple of them even in low end devices won't hurt.ursus
09/30/2019, 8:25 PMursus
09/30/2019, 8:25 PMPablichjenkov
09/30/2019, 9:36 PMflowOn
operator.
I honestly prefer what we have talked about the manual control. That solution abstracts the underlaying machinery. Although requires more time to implement.
It would be interesting doing so.
Okhttp do some similar concept internally to limit the amount of concurrent Calls.
https://github.com/square/okhttp/blob/master/okhttp/src/main/java/okhttp3/Dispatcher.kt
They based their implementaion on the fact that an AsyncCall
has a reference to the dispatcher. So when job completes on the AsyncCall it let the dispatcher knows and it keeps pulling from the AsyncCall queue the next Task.
Have no clue on how to make an equivalent using the Flow API. 🤔 let me think.