bansa icon indicating copy to clipboard operation
bansa copied to clipboard

Is it safe to call dispatch from multiple threads?

Open alukito opened this issue 8 years ago • 6 comments

Hello, thank you for introducing Redux to Android world.

I'm using bansa with Anvil and I think they are a very good match. But I noticed one thing, if we call BaseStore.dispatch() from multiple thread, it's possible to get inconsistent state within one dispatch cycle, since we can't pass the new state to Anvil.render(). We could only use BaseStore.getState() in each view.

I noticed that you use locks in this line and getState(). But they both lock on different objects. synchronized(this) locks on Middleware instance while public synchronized S getState() locks on BaseStore instance.

But I think although we use locks, there is still a chance for inconsistent state since dispatch and getState will not be called in one block.

Do you think we should only call dispatch in one thread? Maybe using a single background thread to do it?

I'll be grateful to hear your thoughts about this. Thank you very much.

alukito avatar Jun 21 '16 03:06 alukito

If you are into Kotlin you can look at my fork of Bansa here: https://github.com/beyondeye/Reduks More specifically look at my implementation of a State Store based on Kovenant promises: https://github.com/beyondeye/Reduks/blob/master/reduks-kovenant/src/main/kotlin/com/beyondeye/reduks/KovenantStore.kt

beyondeye avatar Jun 23 '16 13:06 beyondeye

it solves the problem you rise

beyondeye avatar Jun 23 '16 13:06 beyondeye

Hello @beyondeye, thank you for introducing your project. I think it's really promising. Unfortunately for us, we couldn't introduce Kotlin to our on going project right now.

On personal level though, I'll definitely check Reduks out.

alukito avatar Jun 24 '16 00:06 alukito

Interesting problem! Sorry, been super busy the past couple weeks with work and travel. I'll create some test cases soon to play around with the issue and try out some solutions.

Thanks for raising the issue!

brianegan avatar Jun 24 '16 09:06 brianegan

@brianegan Are there any news on this problem?

vitusortner avatar Oct 05 '17 13:10 vitusortner

@vitusortner I haven't been developing this library as much lately! However, I'm happy to accept PRs if you'd like to take a crack at solving it.

brianegan avatar Oct 05 '17 20:10 brianegan