RxJava data from DB with onscreen list

杀马特。学长 韩版系。学妹 提交于 2019-12-25 08:04:57

问题


I've just begun to learn RxJava and I'm a little bit lost.

My scenario is the following: A local NoSQL key-value database where I store some data. The problem is that the endpoint can add data to this DB and also the user can delete some of it.

I'm displaying this data as an on-screen list (RecyclerView).

I'd like to know what is the best approach to always know what's the most up to date data from the DB in a single place so I can update the UI accordingly.


回答1:


What you're looking for is a way to create an Observer, meaning to transform DB changes events to Observable.

So you will have 2 kind of streams:
One that act on the DB and changes data (update/delete) triggered upon various events (push/cloud/user clicks) , those changes will trigger DB change event, that in it's turn, will emit events on an Second stream that represent DB changes event.

Then in your UI, you can react to the changes in Rx way (responding to the stream of DB changes events).

In order to create the DB changes Observable, you need to learn about hot to create Observable from async events (can be done using Subjects, or if you are integrating with some DB and you have DB changes events, you can 'wrap' it with Observable using fromEmitter(), you can learn more about it from this blog:
https://medium.com/yammer-engineering/converting-callback-async-calls-to-rxjava-ebc68bde5831#.z1cj7ayhr




回答2:


One way to approach it would be to put a data service between clients and the data store. Make that data service an Observable. Allow anyone who is interested in being notified when data changes to register with the data service.



来源:https://stackoverflow.com/questions/42225722/rxjava-data-from-db-with-onscreen-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!