realm-java icon indicating copy to clipboard operation
realm-java copied to clipboard

RxJava support

Open jenzz opened this issue 11 years ago • 46 comments

I'm not sure if the Realm internals allow for RxJava support, but it would be nice to be able to do something like this:

// Get observable of all users
Realm.getInstance(context)
    .where(User.class)
    .findAll()
    .observable()
    .subscribe(users -> {
        // do stuff with users
    });

// Get observable of a single user
Realm.getInstance(context)
    .where(User.class)
    .findFirst()
    .observable()
    .subscribe(user -> {
        // do stuff with user
    });

// Get observable of users table count
Realm.getInstance(context)
    .where(User.class)
    .findAll()
    .size()
    .observable()
    .subscribe(count -> {
        // do stuff with count
    });

jenzz avatar Feb 18 '15 00:02 jenzz

+1

kakkoyun avatar Feb 18 '15 07:02 kakkoyun

We definitely have RxJava on the radar, as it quite a cool framework. A first step for us is probably documenting what works and what doesn't when combining Realm and RxJava, but going forward adding some sort of support is not out of the question.

That said we are currently focusing on other features right now, so I cannot give you any timeframe just yet.

cmelchior avatar Feb 18 '15 14:02 cmelchior

+1

DHuckaby avatar Feb 25 '15 22:02 DHuckaby

Some resources:

  • https://github.com/ReactiveX/RxJava/wiki
  • http://blog.danlew.net/2014/09/15/grokking-rxjava-part-1/
  • https://www.youtube.com/watch?v=_t06LRX0DV0
  • http://mttkay.github.io/blog/2013/08/25/functional-reactive-programming-on-android-with-rxjava/

POC with thoughts:

  • https://github.com/realm/realm-java/tree/cm-lab-rxjava

cmelchior avatar Feb 26 '15 18:02 cmelchior

+1

Internal RxJava support would be great. For now I'm using it with Realm by following several restrictions:

  • One observable for one transaction.
  • Mapping Realm objects to non-Realm (e.g. AutoValue) when passing between observer/subscriber threads.

kboyarshinov avatar Feb 28 '15 18:02 kboyarshinov

Hi @kboyarshinov Can you elaborate a bit on one observable pr. transaction?

I have been evaluating RxJava support as well, and the biggest blockers I can see so far is that we need fine-grained notifications to observe on queries and that it is not possible to move RealmObjects between threads which conflict with Observable.subscribeOn and Observerable.oberserveOn().

The second part is probably the biggest issue.

cmelchior avatar Feb 28 '15 20:02 cmelchior

@cmelchior I implemented OnSubscribeRealm function which controls creation and closing of Realm instance following Observable contract. And it defines custom function to manipulate data with that instance in a single transaction.

One observable per transaction means every RealmObservable operates its own instance of Realm and executes used defined function with this instance in isolated transaction.

Also there is a RealmObservable helper class - it simplifies usage of OnSubscribe function. Full example is also available.

kboyarshinov avatar Mar 01 '15 17:03 kboyarshinov

+1

bernhardpflug avatar Mar 23 '15 14:03 bernhardpflug

+1

tomekpiotrowski avatar Mar 25 '15 15:03 tomekpiotrowski

+1!

Fi5t avatar Apr 01 '15 14:04 Fi5t

+1!!!

colintheshots avatar Apr 08 '15 19:04 colintheshots

+1

imallan avatar Apr 19 '15 13:04 imallan

+1

wasabeef avatar Apr 19 '15 16:04 wasabeef

+1

PavelSynek avatar Apr 26 '15 11:04 PavelSynek

+1 too

EminYahyayev avatar May 01 '15 16:05 EminYahyayev

+1

itrethan avatar May 18 '15 07:05 itrethan

+1

alliannas avatar May 26 '15 20:05 alliannas

+1

JonathonFry avatar May 27 '15 09:05 JonathonFry

+1

ar-g avatar May 30 '15 20:05 ar-g

+1

TheFinestArtist avatar Jun 04 '15 14:06 TheFinestArtist