RxJava data from DB with onscreen list

2019-09-14 01:46发布

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.

2条回答
2楼-- · 2019-09-14 02:15

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

查看更多
smile是对你的礼貌
3楼-- · 2019-09-14 02:30

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.

查看更多
登录 后发表回答