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: Documentation/Schedulers.md
+26-26Lines changed: 26 additions & 26 deletions
Original file line number
Diff line number
Diff line change
@@ -5,65 +5,65 @@ Schedulers
5
5
1.[Custom schedulers](#custom-schedulers)
6
6
1.[Builtin schedulers](#builtin-schedulers)
7
7
8
-
Schedulers abstract away mechanism for performing work.
8
+
Schedulers abstract away the mechanism for performing work.
9
9
10
-
Different mechanisms for performing work include, current thread, dispatch queues, operation queues, new threads, thread pools, run loops ...
10
+
Different mechanisms for performing work include the current thread, dispatch queues, operation queues, new threads, thread pools, and run loops.
11
11
12
-
There are two main operators that work with schedulers.`observeOn` and `subscribeOn`.
12
+
There are two main operators that work with schedulers,`observeOn` and `subscribeOn`.
13
13
14
-
If you want to perform work on different scheduler just use `observeOn(scheduler)` operator.
14
+
If you want to perform work on a different scheduler just use `observeOn(scheduler)` operator.
15
15
16
16
You would usually use `observeOn` a lot more often then `subscribeOn`.
17
17
18
-
In case `observeOn` isn't explicitly specified, work will be performed on which ever thread/scheduler elements are generated.
18
+
In case `observeOn` isn't explicitly specified, work will be performed on whichever thread/scheduler elements are generated.
19
19
20
-
Example of using `observeOn` operator
20
+
Example of using the `observeOn` operator:
21
21
22
22
```
23
23
sequence1
24
24
.observeOn(backgroundScheduler)
25
25
.map { n in
26
-
print("This is performed on background scheduler")
26
+
print("This is performed on the background scheduler")
27
27
}
28
28
.observeOn(MainScheduler.instance)
29
29
.map { n in
30
-
print("This is performed on main scheduler")
30
+
print("This is performed on the main scheduler")
31
31
}
32
32
```
33
33
34
34
If you want to start sequence generation (`subscribe` method) and call dispose on a specific scheduler, use `subscribeOn(scheduler)`.
35
35
36
-
In case `subscribeOn` isn't explicitly specified, `subscribe` method will be called on the same thread/scheduler that`subscribeNext` or `subscribe` is called.
36
+
In case `subscribeOn` isn't explicitly specified, the `subscribe` method will be called on the same thread/scheduler on which`subscribeNext` or `subscribe` is called.
37
37
38
-
In case `subscribeOn` isn't explicitly specified, `dispose` method will be called on the same thread/scheduler that initiated disposing.
38
+
In case `subscribeOn` isn't explicitly specified, the `dispose` method will be called on the same thread/scheduler that initiated disposing.
39
39
40
40
In short, if no explicit scheduler is chosen, those methods will be called on current thread/scheduler.
41
41
42
42
# Serial vs Concurrent Schedulers
43
43
44
44
Since schedulers can really be anything, and all operators that transform sequences need to preserve additional [implicit guarantees](GettingStarted.md#implicit-observable-guarantees), it is important what kind of schedulers are you creating.
45
45
46
-
In case scheduler is concurrent, Rx's `observeOn` and `subscribeOn` operators will make sure everything works perfect.
46
+
In case the scheduler is concurrent, Rx's `observeOn` and `subscribeOn` operators will make sure everything works perfectly.
47
47
48
48
If you use some scheduler that for which Rx can prove that it's serial, it will able to perform additional optimizations.
49
49
50
-
So far it only performing those optimizations for dispatch queue schedulers.
50
+
So far it only performs those optimizations for dispatch queue schedulers.
51
51
52
-
In case of serial dispatch queue schedulers `observeOn` is optimized to just a simple `dispatch_async` call.
52
+
In case of serial dispatch queue schedulers,`observeOn` is optimized to just a simple `dispatch_async` call.
53
53
54
54
# Custom schedulers
55
55
56
56
Besides current schedulers, you can write your own schedulers.
57
57
58
-
If you just want to describe who needs to perform work immediately, you can create your own scheduler by implementing `ImmediateScheduler` protocol.
58
+
If you just want to describe who needs to perform work immediately, you can create your own scheduler by implementing the `ImmediateScheduler` protocol.
In case scheduler doesn't support `PeriodicScheduling` capabilities, Rx will emulate periodic scheduling transparently.
89
+
In case the scheduler doesn't support `PeriodicScheduling` capabilities, Rx will emulate periodic scheduling transparently.
90
90
91
91
# Builtin schedulers
92
92
93
93
Rx can use all types of schedulers, but it can also perform some additional optimizations if it has proof that scheduler is serial.
94
94
95
-
These are currently supported schedulers
95
+
These are the currently supported schedulers:
96
96
97
97
## CurrentThreadScheduler (Serial scheduler)
98
98
99
99
Schedules units of work on the current thread.
100
100
This is the default scheduler for operators that generate elements.
101
101
102
-
This scheduler is also sometimes called `trampoline scheduler`.
102
+
This scheduler is also sometimes called a "trampoline scheduler".
103
103
104
-
If `CurrentThreadScheduler.instance.schedule(state) { }` is called for first time on some thread, scheduled action will be executed immediately and hidden queue will be created where all recursively scheduled actions will be temporarily enqueued.
104
+
If `CurrentThreadScheduler.instance.schedule(state) { }` is called for the first time on some thread, the scheduled action will be executed immediately and a hidden queue will be created where all recursively scheduled actions will be temporarily enqueued.
105
105
106
-
If some parent frame on call stack is already running `CurrentThreadScheduler.instance.schedule(state) { }`, scheduled action will be enqueued and executed when currently running action and all previously enqueued actions have finished executing.
106
+
If some parent frame on the call stack is already running `CurrentThreadScheduler.instance.schedule(state) { }`, the scheduled action will be enqueued and executed when the currently running action and all previously enqueued actions have finished executing.
107
107
108
108
## MainScheduler (Serial scheduler)
109
109
110
-
Abstracts work that needs to be performed on `MainThread`. In case `schedule` methods are called from main thread, it will perform action immediately without scheduling.
110
+
Abstracts work that needs to be performed on `MainThread`. In case `schedule` methods are called from main thread, it will perform the action immediately without scheduling.
111
111
112
112
This scheduler is usually used to perform UI work.
Abstracts the work that needs to be performed on a specific `dispatch_queue_t`. It will make sure that even if concurrent dispatch queue is passed, it's transformed into a serial one.
116
+
Abstracts the work that needs to be performed on a specific `dispatch_queue_t`. It will make sure that even if a concurrent dispatch queue is passed, it's transformed into a serial one.
117
117
118
118
Serial schedulers enable certain optimizations for `observeOn`.
119
119
120
-
Main scheduler is an instance of `SerialDispatchQueueScheduler`.
120
+
The main scheduler is an instance of `SerialDispatchQueueScheduler`.
Abstracts the work that needs to be performed on a specific `dispatch_queue_t`. You can also pass a serial dispatch queue, it shouldn't cause any problems.
125
125
126
-
This scheduler is suitable when some work needs to be performed in background.
126
+
This scheduler is suitable when some work needs to be performed in the background.
127
127
128
128
## OperationQueueScheduler (Concurrent scheduler)
129
129
130
130
Abstracts the work that needs to be performed on a specific `NSOperationQueue`.
131
131
132
-
This scheduler is suitable for cases when there is some bigger chunk of work that needs to be performed in background and you want to fine tune concurrent processing using `maxConcurrentOperationCount`.
132
+
This scheduler is suitable for cases when there is some bigger chunk of work that needs to be performed in the background and you want to fine tune concurrent processing using `maxConcurrentOperationCount`.
0 commit comments