本文介绍了RxJava - 观察可能始终发生变化的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 限时删除!! 我正试图加入反应潮流,但在阅读并经历了很多例子之后,我仍然没有找到我正在寻找的东西。 更改可能来自更新它的特定请求(来自服务器,数据库等)或者它可能由于在应用程序中触发的事件而得到更新。 我的问题是如何创建 Observable 等一个对象,如何在有变化的情况下继续更新订阅者? 从我到目前为止看到的我可以像这样创建observable: Observable.create(new Observable.OnSubscribe< MyModel>(){ @Override public void call(final Subscriber< ;?super MyModel> subscriber){ subscriber.onNext(MyModel instance); } }); 我在这里缺少的是: 我不想发出不同的值( MyModel 的实例),但只是想让订阅者知道同一个实例(他们订阅的那个已经改变了。 如果我理解正确,那么调用方法,但这不是我需要的,我需要在有更新时才采取行动,然后我想通知所有订户。 很有可能我错了,这就是为什么我很高兴能够理解如何用RxJava来满足我的需求。 谢谢。解决方案我认为做这样的事情的最佳选择是主题。所以你可以这样: public static class MyModel { private PublishSubject< MyModel> changeObservable = PublishSubject.create(); private String field1; public String getField1(){ return field1; } public void setField1(String field1){ this.field1 = field1; changeObservable.onNext(this); } public Observable< MyModel> getModelChanges(){返回changeObservable; } @Override public String toString(){ returnMyModel {+ field1 ='+ field1 +'\ ''+ '}'; } } public static void main(String [] args){ MyModel myModel = new MyModel(); myModel.getModelChanges() .subscribe(System.out :: println); myModel.setField1(1); myModel.setField1(2); myModel.setField1(3); } 因此,模型中的所有设置者都会通知模型中的某些内容已发生变化所有订阅者都将获得该数据。 I'm trying to jump on the reactive bandwagon, but after reading and going through lots of examples, I still haven't found what I'm looking for.I have a model object which might get change during any time in the lifecycle of the application.The changes might come from a specific request to update it (from servers, db, etc) or it might get updated due an event which fires in the app.My question is how do I create an Observable of such an object and how do I keep updating the subscribers when there's a change?From what I've seen so far I can create the observable like so:Observable.create(new Observable.OnSubscribe<MyModel>() { @Override public void call(final Subscriber<? super MyModel> subscriber) { subscriber.onNext(MyModel instance); }});What I'm missing here:I do not want to emit different values (instances of MyModel) but just want to let the subscribers know that the same instance (the one on which they subscribed on) has changed.If I understand it right, then the call method is invoked whenever a new subscriber has registered, but that's not what I need, I need to take action only when there's an update and then I want to notify ALL subscribers.There's a good chance that I just got this all wrong, which is why I'd be happy to understand how it's possible to accomplish my needs with RxJava.Thanks. 解决方案 I think the best option to do stuff like that is Subjects. So you can have something like this:public static class MyModel { private PublishSubject<MyModel> changeObservable = PublishSubject.create(); private String field1; public String getField1() { return field1; } public void setField1(String field1) { this.field1 = field1; changeObservable.onNext(this); } public Observable<MyModel> getModelChanges() { return changeObservable; } @Override public String toString() { return "MyModel{" + "field1='" + field1 + '\'' + '}'; }}public static void main(String[] args) { MyModel myModel = new MyModel(); myModel.getModelChanges() .subscribe(System.out::println); myModel.setField1("1"); myModel.setField1("2"); myModel.setField1("3");}So all setters in the model will notify that something has changed in your model and all subscribers will get that data. 这篇关于RxJava - 观察可能始终发生变化的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 1403页,肝出来的..
09-07 20:06