Subject & Schedulers

Subject & Schedulers
A Subject is a sort of bridge or proxy that is available in some implementations of ReactiveX that acts both as an observer and as an Observable. Because it is an observer, it can subscribe to one or more Observables,and because it is an Observable,it can pss throuth the items it observes by reemitting them, and it can emit new items.
Because a Subject subscribes to an Observable,it will trigger that Observable to begin emitting items (if that Observable is “code”-that is ,if it waits for a subscription before it begins to emit items.)Thiscan have the effect of making the resulting Subject a ‘hot’ Observable variant of the original ‘code’ Observable.

(Serial vs Concurrent Schedulers & Custom schedulers & Builtin schedulers)
Schedulers abstract away the mechanism for performing work.
Different mechanisms for performing work include the current thread, dispatch queues, operation queues, new threads, thread pools, and run loops.
There are two main operators that work with schedulers /observeOn and subscribeOn.
If you want to perform work on a different scheduler just use observeOn(scheduler) operator.
You would usually use observeOn a lot more often than subscribeOn.
In case observeOn isn’t explicitly specified, work will be performed on whichever thread/scheduler elements are generated.
Example of using the observeOn operator:

sequence1
.observeOn(backgroundScheduler)
.map { n in
print(“This is performed on the background scheduler”)
}
.observeOn(MainScheduler.instance)
.map { n in
print(“This is performed on the main scheduler”)
}

custom schedulers

public protocol ImmediateScheduler {

  func scheduler(state: StateType, action:(StateType) -> Result) -> RxReslt

}

If you want to create a new scheduler that supports time based operations, then you ‘ll need to implement the scheduler protocol:

public protocol Scheduler: ImmediateScheduler {
associatedType TimeInterval
associaltedType Time

 var now: Time {
       get

}

func sheduleRelative(state: StateType, dueTime: TimeInterval, action: (StateType) -> RxResult) -> RxResult
}

public protocol PeriodicScheduler: Scheduler {
func schedulerPeriodic(state: StateType, startAfter: TimeInterval, period: TimeInterval, action: (StateType) -> StateType) -> RxResult
}

Builtin schedulers(Rx can use all types of schedulers, but it can also perform some additional optimizations if it has proof that scheduler is serial.These are the currently supported schedulers:
currentThreadScheduler(Serial scheduler) & MainScheduler(Serial scheduler) & SerialDispatchQueueSecheduler(Serial scheduler) & ConcurrentDispatchQueueSecheduler(Concurrent scheduler) & OperationQueueScheduler(ConcurrentScheduler)
)

referring to: https://github.com/ReactiveX/RxSwift/blob/master

你可能感兴趣的:(iOS)