Daniel T. Daniel T. - 1 year ago 180
Swift Question

How to use ReplaySubject in ReactiveKit

I have this in RxSwift:

func foo() -> Observable<Int> {
let subject = RxSwift.ReplaySubject<Int>.create(bufferSize: 1)
return subject.asObservable()

How do I implement the same concept in ReactiveKit?

Answer Source

With ReactiveKit 3 that is currently in beta (rk3 branch) you can do:

func foo() -> Signal<Int, NoError> {
  let subject = ReplaySubject<Int, NoError>(bufferSize: 1)
  return subject.toSignal()

In ReactiveKit 2 ReplaySubject is generalised over events:

func foo() -> Stream<Int> {
  let subject = ReplaySubject<StreamEvent<Int>>(bufferSize: 1)
  return Stream(rawStream: subject.toRawStream())


func foo() -> Operation<Int, Error> {
  let subject = ReplaySubject<OperationEvent<Int>>(bufferSize: 1)
  return Operation(rawStream: subject.toRawStream())
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download