I've got a list of objects that have Deferred prop...
# coroutines
a
I've got a list of objects that have Deferred properties which produce some kind of reult, afteer result is produced, the Deferred is replaced by another (Completable)Deferred. Now I want to create a listener which will perform some action when any future is completed. I am trying to do so using
select
clause this way:
Copy code
val job = launch {
            while (true) {
                select<Unit> {
                    stateHolder.forEach { state ->
                        state.future.onAwait {
                                // do something
                        }
                    }
                }
            }
        }
But it does not seem to enter the select clause even once. What am I doing wrong?
g
Did you try to debug?
a
Of course. I found it to be a some bizarre synchronization problem, which i did not expect to encounter in coroutines. Everything works fine if I introduce blocking sleep between state changes which are done from outside of coronations. Probably should use
Mutex
locks. In fact, I keep thinking that it should be simpler. All I need is to create an observable mutable state with coroutine based subscribers.
It seems that I really need
BroadcastChannel
. I missed it previously.
g
Coroutines do not solve syncronous problems if you have shared mutable states between threads, bit with coroutines you can use actors or other patterns to handle this
a
I do not have problems with concurrent state changes, the problem was with notifications. I finally solved it with
BroadcastChannel
. Everything seems to be working fin now.