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<StateType>(state: StateType, action:(StateType) -> Result<Disposable>) -> RxReslt<Disposable>

}

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值