Guest User

Untitled

a guest
Jan 18th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.80 KB | None | 0 0
  1. Schedulers
  2. ==========
  3.  
  4. 1. [Serial vs Concurrent Schedulers](#serial-vs-concurrent-schedulers)
  5. 1. [Custom schedulers](#custom-schedulers)
  6. 1. [Builtin schedulers](#builtin-schedulers)
  7.  
  8. Schedulers abstract away the mechanism for performing work.
  9.  
  10. Different mechanisms for performing work include the current thread, dispatch queues, operation queues, new threads, thread pools, and run loops.
  11.  
  12. There are two main operators that work with schedulers, `observeOn` and `subscribeOn`.
  13.  
  14. If you want to perform work on a different scheduler just use `observeOn(scheduler)` operator.
  15.  
  16. You would usually use `observeOn` a lot more often than `subscribeOn`.
  17.  
  18. In case `observeOn` isn't explicitly specified, work will be performed on whichever thread/scheduler elements are generated.
  19.  
  20. Example of using the `observeOn` operator:
  21.  
  22. ```
  23. sequence1
  24. .observeOn(backgroundScheduler)
  25. .map { n in
  26. print("This is performed on the background scheduler")
  27. }
  28. .observeOn(MainScheduler.instance)
  29. .map { n in
  30. print("This is performed on the main scheduler")
  31. }
  32. ```
  33.  
  34. If you want to start sequence generation (`subscribe` method) and call dispose on a specific scheduler, use `subscribeOn(scheduler)`.
  35.  
  36. In case `subscribeOn` isn't explicitly specified, the `subscribe` closure (closure passed to `Observable.create`) will be called on the same thread/scheduler on which `subscribe(onNext:)` or `subscribe` is called.
  37.  
  38. In case `subscribeOn` isn't explicitly specified, the `dispose` method will be called on the same thread/scheduler that initiated disposing.
  39.  
  40. In short, if no explicit scheduler is chosen, those methods will be called on current thread/scheduler.
  41.  
  42. # Serial vs Concurrent Schedulers
  43.  
  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.  
  46. In case the scheduler is concurrent, Rx's `observeOn` and `subscribeOn` operators will make sure everything works perfectly.
  47.  
  48. If you use some scheduler that Rx can prove is serial, it will be able to perform additional optimizations.
  49.  
  50. So far it only performs those optimizations for dispatch queue schedulers.
  51.  
  52. In case of serial dispatch queue schedulers, `observeOn` is optimized to just a simple `dispatch_async` call.
  53.  
  54. # Custom schedulers
  55.  
  56. Besides current schedulers, you can write your own schedulers.
  57.  
  58. If you just want to describe who needs to perform work immediately, you can create your own scheduler by implementing the `ImmediateScheduler` protocol.
  59.  
  60. ```swift
  61. public protocol ImmediateScheduler {
  62. func schedule<StateType>(state: StateType, action: (/*ImmediateScheduler,*/ StateType) -> RxResult<Disposable>) -> RxResult<Disposable>
  63. }
  64. ```
  65.  
  66. If you want to create a new scheduler that supports time based operations, then you'll need to implement the `Scheduler` protocol:
  67.  
  68. ```swift
  69. public protocol Scheduler: ImmediateScheduler {
  70. associatedtype TimeInterval
  71. associatedtype Time
  72.  
  73. var now : Time {
  74. get
  75. }
  76.  
  77. func scheduleRelative<StateType>(state: StateType, dueTime: TimeInterval, action: (StateType) -> RxResult<Disposable>) -> RxResult<Disposable>
  78. }
  79. ```
  80.  
  81. In case the scheduler only has periodic scheduling capabilities, you can inform Rx by implementing the `PeriodicScheduler` protocol:
  82.  
  83. ```swift
  84. public protocol PeriodicScheduler : Scheduler {
  85. func schedulePeriodic<StateType>(state: StateType, startAfter: TimeInterval, period: TimeInterval, action: (StateType) -> StateType) -> RxResult<Disposable>
  86. }
  87. ```
  88.  
  89. In case the scheduler doesn't support `PeriodicScheduling` capabilities, Rx will emulate periodic scheduling transparently.
  90.  
  91. # Builtin schedulers
  92.  
  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.  
  95. These are the currently supported schedulers:
  96.  
  97. ## CurrentThreadScheduler (Serial scheduler)
  98.  
  99. Schedules units of work on the current thread.
  100. This is the default scheduler for operators that generate elements.
  101.  
  102. This scheduler is also sometimes called a "trampoline scheduler".
  103.  
  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.  
  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.  
  108. ## MainScheduler (Serial scheduler)
  109.  
  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.  
  112. This scheduler is usually used to perform UI work.
  113.  
  114. ## SerialDispatchQueueScheduler (Serial scheduler)
  115.  
  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.  
  118. Serial schedulers enable certain optimizations for `observeOn`.
  119.  
  120. The main scheduler is an instance of `SerialDispatchQueueScheduler`.
  121.  
  122. ## ConcurrentDispatchQueueScheduler (Concurrent scheduler)
  123.  
  124. 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.  
  126. This scheduler is suitable when some work needs to be performed in the background.
  127.  
  128. ## OperationQueueScheduler (Concurrent scheduler)
  129.  
  130. Abstracts the work that needs to be performed on a specific `NSOperationQueue`.
  131.  
  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`.
Add Comment
Please, Sign In to add comment