brianegan / bansa

A state container for Java & Kotlin, inspired by Redux & Elm
MIT License
444 stars 29 forks source link

Is it safe to call dispatch from multiple threads? #24

Open alukito opened 8 years ago

alukito commented 8 years ago

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.

beyondeye commented 8 years ago

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 commented 8 years ago

it solves the problem you rise

alukito commented 8 years ago

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.

brianegan commented 8 years ago

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!

vitusortner commented 6 years ago

@brianegan Are there any news on this problem?

brianegan commented 6 years ago

@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.