ReSwift
ReSwift copied to clipboard
Crash when dispatching actions from inside StoreSubscriber.newState()

We have several actions that are chained one after another in this manner:
- Action is dispatched
- A StoreSubscriber receives
newStatecallback on state change - It dispatches another Action on the same Store
- Another StoreSubscriber (or maybe the same one) receives
newStatecallback on state change - It dispatches another Action on the same Store
These chains are not infinitely recursive, their depth is 2-4 actions. They all operate on single (main) thread synchronously. We see a crash when 2nd or 3rd dispatch is called inside ReSwift. It occurs with different action sequences. One case we can reproduce 100% in debug configuration (-Onone), but it's not reproduced in release configuration.
Stacktrace top is __swift_instantiateConcreteTypeFromMangledName
I recommend against this pattern -- It could be running into call-stack or memory-stack restrictions, due to not allowing the runloop to complete and reset.
If you need to, dispatching the chained actions should resolve the issue, as they'll occur on the next runloop and have the available resources again.
Better is to handle the state changes within the singular, first action. If its all synchronous, then technically there is no need for the follow-up actions. You can add the first action to the reducers appropriately.
@mjarvis This would be an example of (ab)using ReSwift as a general-purpose tool to decouple and implement messaging between components, something we brushed a couple of years ago, right?