This is my attempt to create an RxRelay implementation in RxSwift.
Because the Observer Design pattern is something every developer should know. It facilitates the communication between objects and simplify the logic of your app. Implementing it in a Reactive way will take it to a whole new level.
Currently the recommended installation method is Cocoapods
- In your
Podfile- Add this
pod 'RxRelay'
- Add this
-
BehaviorRelayRelay that emits the most recent item it has observed and all subsequent observed items to each subscribed
Observer. -
PublishRelayRelay that, once an
Observerhas subscribed, emits all subsequently observed items to the subscriber.
var relay = PublishRelay<String>()
let subscription = relay.subsribe { (string ) in
print("I have Received :\(string)")
}
relay.accept("Hello")
relay.accept("World")
subscription.dispose()
relay.accept("Will not be emitted ")
-
ReplayRelayRelay that buffers all items it observes and replays them to any
Observerthat subscribes.let relay = ReplayRelay<String>(withBufferSize: 2) relay.accept("1") relay.accept("2") relay.accept("3") let subscription = relay.subsribe { (string) in print("I have received : \(string)") } // subscription will get 2 , 3 , 4 relay.accept("4")