A common step in OOP is to make a web request, get data, and refresh the UI
With RxSwift, this step can be broken down into two steps,
Network request, getting data, is one step
Refreshing the UI is step two
Multiple controllers, with the same network request, share and synchronize data
The scenario is useful
- Use notice:
Go make a network request and refresh the screen.
There will be two such instructions
NotificationCenter.default.rx.notification(.readNetRequest)
.takeUntil(self.rx.deallocated)
.subscribe(onNext: { [weak self] ( noti ) in
guard let `self` = self else { return }
self.doReadNetRequestAndDoRefreshUI()
}).disposed(by: rx.disposeBag)
Copy the code
- USES RxSwift:
To request a network, subscribe to the interface to refresh
There is one less network request than using notification.
NetMangerPublisher.f.outputStudy.subscribe(onNext: { (data) in
self.doRefreshUI()
}).disposed(by: rx.disposeBag)
Copy the code
Network requests for read operations are less represented. Network requests for write operations are evident.
Network request management, behavior-driven, requesting data when needed,
Data driven, data update, UI update.
The UI state is always consistent with the latest data.
This convenient realization of data sharing
struct NetMangerPublisher {
static var f = NetMangerPublisher()
private let inputRead = BehaviorSubject<Bool>(value: true)
private letinputWrite= PublishSubject<Bool>() var outputRead: Observable<[String: Any]? > var outputWrite: Observable<[String: Any]? >init(){
outputRead = inputStudy.asObservable().map({ (_) -> Bool inGlobal.hasLogined() }) .flatMapLatest { isRefreshing -> Observable<[String: Any]? >in
guard isRefreshing else { return .empty() }
returnNetManager.standard.read() }.share(replay: 2, scope: SubjectLifetimeScope.forever) outputWrite = inputClock.asObservable().flatMapLatest{ (isRefreshing) -> Observable<[String: Any]? >in
return NetManager.standard.write()
}.share(replay: 4, scope: SubjectLifetimeScope.forever)
}
func doRead(){
inputRead.onNext(true)
}
func doWrite(){
inputWrite.onNext(true)}}Copy the code
BehaviorSubject, which can be set to default values. Because you need an initial interface, a network request in viewDidLoad. An initial state
PublishSubject, suitable for user operations. Nothing happens when the user doesn’t do it.
Simple example of RxSwift creating an Observable:
let disposeBag = DisposeBag()
Observable<String>.create { observer in
observer.onNext("1")
observer.onCompleted()
return Disposables.create()
}
.subscribe(
onNext: { print($0) },
onError: { print($0) },
onCompleted: { print("Completed") },
onDisposed: { print("Disposed") }
)
.disposed(by: disposeBag)
Copy the code
The create method defines all the events that are sent to the subscriber
The create method has a trailing closure with a generic AnyObserver that acts as an observer observer
The Observer can add events to form a sequence of events that can be sent to subscribers
The network example above:
func readData() -> Observable<[]>{
returnObservable<[String: Any]? >.create{ (observer) -> Disposableinself.readDataTask? .cancel() self.readDataTask = Base.netHud.request(.readData){ (result)in/ /... observer.onNext(dat) observer.onCompleted() }returnDisposables.create{ self.readDataTask? .cancel() } } }Copy the code