You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: website/docs/introduction/Threading.md
+53-6
Original file line number
Diff line number
Diff line change
@@ -7,24 +7,71 @@ hide_title: true
7
7
8
8
# Redux on Multi-threaded Platforms
9
9
10
+
TLDR; use [createThreadSafeStore()](../api/createThreadSafeStore.md), unless your Javascript only
11
+
10
12
Redux in multi-threaded environments brings additional concerns that are not present in redux
11
13
for Javascript. Javascript is single threaded, so Redux.js did not have to address the issue.
12
14
Android, iOS, and native do have threading, and as such attention must be paid to which threads interact with the store.
13
15
14
-
As of ReduxKotlin 0.3.0 there is `same thread enforcement` for the [getState](../api/store#getstate-_or_-state-property), [dispatch](../api/store#dispatchaction-any-any), [replaceReducer](../api/store#replacereducernextreducer-reducer-state-unit),
15
-
and [subscribe](../api/store#subscribelistener-storesubscriber) functions on the store. This means these methods must be called from the same thread where
16
+
In a multi-threaded system invalid states and race conditions can happen.
17
+
For example if `getState` was called at the same time as a dispatch, the state could represent a past
18
+
state. Or 2 actions dispatched concurrently could cause an invalid state.
19
+
20
+
**So you there are 3 options:**
21
+
22
+
1) Synchronize access to the store - [createThreadSafeStore()](../api/createThreadSafeStore.md)
23
+
2) Only access the store from the same thread - [createSameThreadEnforcedStore()](../api/createSameThreadEnforcedStore.md)
24
+
3) Live in the wild west and access store anytime, any thread and live with consequences - NOT RECOMMENDED - [createStore()](../api/createStore.md)
25
+
26
+
ReduxKotlin allows all these, but most cases will fall into #1.
27
+
28
+
Starting with ReduxKotlin 0.5.0 there is a threadsafe store which uses synchronization (AtomicFu library)
29
+
to allow safe access to all the functions on the store. This is the recommended usage for 90% of use cases.
30
+
31
+
```kotlin
32
+
val store = createThreadSafeStore(...)
33
+
```
34
+
35
+
Who is the other 10%? If you are only targeting Javascript thread safety is not an issue, so
36
+
`createStore` is the way to go. Other possible reasons could be applications that need optimal
37
+
performance, however it is unlikely that the extra overhead from synchronization will be an issue.
38
+
39
+
## Same thread enforcement
40
+
41
+
Another option is `same thread enforcement`. This means these methods must be called from the same thread where
16
42
the store was created. An `IllegalStateException` will be thrown if one of these are called from a
17
43
different thread.
18
44
19
-
If this `same thread enforcement` was not in place invalid states and race conditions could happen.
20
-
For example if `getState` was called at the same time as a dispatch, the state would represent a past
21
-
state. Or 2 actions dispatched concurrently could cause an invalid state.
45
+
46
+
`Same thread enforcement` was the default behavior for ReduxKotlin 0.3.0 - 0.4.0.
22
47
23
48
Note that this is __SAME__ thread enforcement - not __MAIN__ thread enforcement. ReduxKotlin does not
24
49
force you to use the main thread, although you can if you'd like. Most mobile applications do redux on the main
25
50
thread, however it could be moved to a background thread. Using a background thread could be desirable
26
51
if the reducers & middleware processing produce UI effects such as dropped frames.
27
52
28
-
29
53
Currently `same thread enforcement` is implemented for JVM, iOS, & macOS. The other platforms
30
54
have do not have the enforcement in place yet.
55
+
56
+
To use `same thread enforcement`:
57
+
```kotlin
58
+
val store = createSameThreadEnforcedStore(...)
59
+
```
60
+
61
+
62
+
***IF*** you are using vanilla `createStore()`, then you may use an different artifact,
0 commit comments