rxjava 观察者模式
For a min, don’t think about Rxjava. If, in general, I ask you what is observable, what would you say? Lets search on google
一分钟,不要考虑Rxjava。 如果总的来说,如果我问你什么是可观察的,你会说什么? 让我们在谷歌搜索
According to google:
根据谷歌:
Observer 观察者Observable:- able to be noticed or perceived;
可观察:-能够被注意到或感知;
Observer:- a person who watches or notices something.
观察者:观察或注意到某物的人。
Can I say here, observable is something that can be observed. Or observable is a source that we can observe for something?
我可以在这里说,可观察是可以观察到的。 还是可以观察到的我们可以观察到的东西?
For what observable can be observed, it could be any data. For example, you are watching movies on your laptop, so here your computer is observable that is emitting movies, and you are the observer who is receiving that data.
对于可以观察到的东西,可以是任何数据。 例如,您正在笔记本电脑上观看电影,因此在这里可以观察到计算机正在播放电影,并且您是正在接收该数据的观察者。
Similarly, in RxJava, Observable is something that emits some data or event, and an observer is something that receives that data or event.
同样,在RxJava中,Observable是发出一些数据或事件的对象,而观察者是接收该数据或事件的对象。
Note: I will be using Kotlin code examples in this post.
注意:我将在本文中使用Kotlin代码示例。
Let’s create a simple observable :
让我们创建一个简单的observable:
val observable: Observable<T> = Observable.just(item : T)Here T could be of any type like a string, char, int, or even a list. What observable will do here is, it will emit item T. There are other ways to emit items, we will see later in this post.
这里T可以是任何类型,例如字符串,char,int甚至是列表。 这里可以观察到的是,它将发射项目T。还有其他发射项目的方法,我们将在本文后面看到。
How do Observable works???.
可观察的工作原理。
An Observable works through its onNext(), onCompleted(), and onError() calls.
一个Observable通过其onNext(),onCompleted()和onError()调用工作。
At the highest level, an Observable works by passing three types of events:
在最高级别,可观察项通过传递三种类型的事件来工作:
onNext(T):- used to emit item(of type T) one at a time all the way down to the observer
onNext(T):-一次一次向观察者发射一个(T型)物品
onComplete():- communicates that all data has been emitted or indicates that no item will be emitted after this call.
onComplete():-表示已发出所有数据,或指示在此调用之后将不发出任何项目。
onError():- communicates an error onError():-传达错误Let’s see how can we emit a string
让我们看看如何发出一个字符串
val observable: Observable<String> = Observable.just("Hello")Now let’s see how to receive this string using subscribe (will discuss just in few moments):
现在,让我们看看如何使用订阅接收此字符串(将在稍后讨论):
observable.subscribe { s -> // received string s hereLog.d(TAG, "received string:- $s")}Output
输出量
received string:- Hello
收到的字符串:-您好
Let’s start with creating a source Observable using Observable.create() and see how we can use onNext() to emit data(say some string):
让我们从使用Observable.create()创建一个源Observable开始,看看如何使用onNext()发出数据(比如说一些字符串):
val observable: Observable<String> = Observable.create<String> { it.onNext("hello")it.onNext("Kotlin")it.onComplete()}Use subscribe to receive these string :
使用subscription接收以下字符串:
observable.subscribe { s ->Log.d(TAG, "received string:- $s")}Output
输出量
received string:- hello
收到的字符串:-你好
received string:- Kotlin
收到的字符串:-Kotlin
You may be wondering we have only seen how is observable emitting data. We are still not sure where the observer is, how it works? So what is this subscription thing here?
您可能想知道我们只看到了如何观察到发射数据。 我们仍然不确定观察者在哪里,它是如何工作的? 那么这里的订阅内容是什么?
Let’s see what is subscribe here doing:
让我们看看这里的订阅操作:
Well, subscribe is the method to attach an observer to an observable. For that subscribe method, accept observer object as a parameter.
好吧,订阅是将观察者附加到可观察对象的方法。 对于该订阅方法,接受观察者对象作为参数。
How do we create an observer then?
那么我们如何创建观察者?
The Observer interface
观察者界面
public interface Observer<T> {void onSubscribe(Disposable d);void onNext(T value);void onError(Throwable e);void onComplete();}Let’s understand each one by one:
让我们一一理解:
onNext(T value): Here, we will be receiving T value emitted by observable.
onNext(T value):在这里,我们将接收observable发出的T值。
onError(Throwable e): Used for error handling
onError(Throwable e):用于错误处理
onComplete(): Called when observable is done emitting items.
onComplete():在完成可观察的发射项目时调用。
onSubscribe(): here we get disposable, which will be used to dispose of the stream, or we can say to unsubscribe the observable.
onSubscribe():在这里我们得到了可抛弃的对象,它将用于处理流,或者可以说取消订阅可观察对象。
Now let’s get back to the previous example :
现在让我们回到前面的示例:
Observable:
可观察的:
val observable: Observable<String> = Observable.create<String> { it.onNext("hello")it.onNext("Kotlin")it.onComplete()}Create Observer:
创建观察者:
val observer: Observer<String> = object : Observer<String> { override fun onComplete() { // onComplete called of observer } override fun onSubscribe(d: Disposable) { // onSubscribe called of observer } override fun onNext(t: String) { // onNext called of observerLog.d(TAG, "received string: $t ") } override fun onError(e: Throwable) { // onError called of observer }}Pass this observer to subscribe :
通过该观察员订阅:
observable.subscribe(observer)Output
输出量
received string:- hello
收到的字符串:-你好
received string:- Kotlin
收到的字符串:-Kotlin
Instead of creating observer like above, shortened the observer by lambda:
与其像上面那样创建观察者,不如将观察者缩短lambda:
observable.subscribe({ // onNext called of observer Log.d(TAG, "received string: $t ") },{ // onError called of observer },{ // onComplete called of observer },{ // onSubscribe called of observer })Here the output will be the same.
在这里输出将是相同的。
Can we reduce more? Off cause, we can. All these functions are optional, and we can only pass the lambda for receiving item like below:
我们可以减少更多吗? 当然可以。 所有这些功能都是可选的,我们只能传递lambda来接收项目,如下所示:
observable.subscribe({ s ->Log.d(TAG, "received string:- $s")})Here we are implementing only onNext .
在这里,我们仅实现onNext 。
翻译自: https://medium.com/@0202gaurav/observable-vs-observer-rxjava-8739b7612a54
rxjava 观察者模式