1
votes

I'm new to RxJava, and trying to use the Realm Observable feature.

When doing this

realm.where(Intake.class)
            .findAllSorted("time", Sort.DESCENDING)
            .asObservable()

I get the full list of items, but when soemthing changes (ie item added), I get the full list again.

What is the RxJava-way to get only the new items?

Thanks in advance

1

1 Answers

0
votes

You are currently observing the query results. Because your query can contain multiple items (findAllSorted()), you're always observing the RealmResults that will emit all sorted items when there's a change (see docs).

You can do something like this:

realm.where(Intake.class)
    .findAllSorted("time", Sort.DESCENDING)
    .asObservable()
    .flatMapIterable(results -> results)
    .distinct();

This does 2 more things:

  • convert your RealmResults to singular Intake instances
  • only let distinct items pass through (make sure your Intake implements equals() correctly)

This does however impose some extra CPU load, because each time the query passes on a new RealmResult, processing is done to filter out the distinct items.

In the above example, sorting will work on the initial set of emitted Intake objects. However, any subsequent emitted items could be observed out of order because they are new and emitted after the initial results.