airbnb / mavericks

Mavericks: Android on Autopilot
https://airbnb.io/mavericks/
Apache License 2.0
5.83k stars 500 forks source link

Using main thread to avoid race conditions of setting state in MvRxStateStore #167

Closed luozejiaqun closed 5 years ago

luozejiaqun commented 5 years ago

As I mentioned in #79 . stateReducer is just updating the state, why can't put it in main thread. I know the performance advantages of using background threads. But using main thread and avoid creating new threads also has performance advantages.

gpeal commented 5 years ago

@luozejiaqun What are the real world performance concerns you have around creating a new thread? They don't have 0 cost but they have relatively little cost.

gpeal commented 5 years ago

@luozejiaqun Also, what race conditions are you concerned about? The MvRx threading model was pretty carefully considered to avoid getting race conditions.

luozejiaqun commented 5 years ago

@gpeal My thought is very simple. Almost very View has a ViewModel (maybe more than one), and every ViewModel has a StateStore which needs a new thread to update the state. That maybe waste. Updating state is shallow copy, I don't think that will affect performance.

gpeal commented 5 years ago

@luozejiaqun The less work you do on the main thread the better. You were the one who was worried about immutable list performance but this solves that problem for you.