The world is material, material is moving, movement is regular, law can be recognized by human, why not software development movement, attributed to data?
Why is it called monitoring programming
Monitoring programming is the best word I find to describe an Observable. This is crucial to understanding RxSwift.
App development must be data-driven interface, and data sources can be roughly divided into: network, local, screen, button, camera.
If we can watch the data change and monitor all the data that might affect the interface change, then drive the interface according to the data.
This shifts from imperative programming to monitoring programming.
In the world of Rx, everything (data sources) is a monitored object.
Observables distribute a bunch of data
In most cases, the data source is constantly generating new data, and we make adjustments based on the new data (of course, old data is also required, but we’ll leave that out for now).
There are many ways to create a monitored object, but I won’t go into detail here.
We’re only going to look at one, and this is the one that we use the most, the hardest:
Observable.create
Copy the code
Example:
Observable<String>.create { observer in
observer.onNext("1")
return Disposables.create()
}
Copy the code
Observable< generic >.create. Generics depend on the type that the monitor object can emit, which in this case is a String or, for network requests, a JSON dictionary.
Observable more mechanisms
Most monitored objects provide more mechanisms for better monitoring, such as:
- 1. Data is sent
- 2. Data anomalies and errors
- 3. Life cycle
After data is sent: observer.oncompleted () After data is sent, the monitored object automatically ends its life
Observer. onError(error) After a data exception is generated, the monitored object automatically ends its life
Life cycle: if the outside world does not want to monitor, you can end the monitored object, no one monitoring, the monitored object is not working.
All codes are as follows:
let disposeBag = DisposeBag(a)Observable<String>.create { observer in
observer.onNext("1")
observer.onError(error)
observer.onCompleted()
return Disposables.create()
}
.subscribe(
onNext: { print($0) },
onError: { print($0) },
onCompleted: { print("Completed") },
onDisposed: { print("Disposed") }
).disposed(by: disposeBag)
}
Copy the code
Not all monitored objects provide such a mechanism, and there are always requirements for monitored objects to provide different mechanisms.
The official offers us the following:
- Singles emit only one data and then end with only success(value) or error(error).
- The Completable emits only one data, and then it ends, and the data is only completed or error(error)
- Maybe just emits one data and then completes, success(value) or completed or error(error)
There are corresponding scenarios for the above three types. I’ll introduce you later.
Single with network request
The result of a network request is a success or failure. Is it possible to create a monitored object for a network request using Single?
Like this:
let observable: Single"[String: Any] >= Single.create { observable in
.
///
observable.success(x)
.
///
observable.error(x)
return Disposables.create()
}
Copy the code
The answer is no. There is one case that may not be satisfied: the interface data cache. Because once success is issued, the Single object terminates itself.
summary
- Why is it called monitoring programming?
- How do I create an Observable?
- Types and features of observables