Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@mitchwongho
Created February 18, 2016 13:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mitchwongho/545249bf38e8f901d845 to your computer and use it in GitHub Desktop.
Save mitchwongho/545249bf38e8f901d845 to your computer and use it in GitHub Desktop.
RealmResult Observable
package com.github.mitchwongho.android.beacon.database.rx;
import android.content.Context;
import android.support.annotation.NonNull;
import io.realm.Realm;
import io.realm.RealmChangeListener;
import io.realm.RealmObject;
import io.realm.RealmResults;
import rx.Observable;
import rx.Subscriber;
import rx.android.MainThreadSubscription;
/**
*
*/
public class RealmObjectFetchOnSubscribe<T extends RealmObject> implements Observable.OnSubscribe<RealmResults<T>> {
private Context context;
private Class<T> clazz;
public RealmObjectFetchOnSubscribe(@NonNull final Context context, @NonNull final Class<T> clazz) {
this.clazz = clazz;
this.context = context;
}
@Override
public void call(@NonNull final Subscriber<? super RealmResults<T>> subscriber) {
final Realm realm = Realm.getInstance(context);
final RealmResults<T> results = realm.where(clazz).findAll();
final RealmChangeListener changeListener = new RealmChangeListener() {
@Override
public void onChange() {
if (!subscriber.isUnsubscribed()) {
subscriber.onNext(results);
}
}
};
results.addChangeListener(changeListener);
subscriber.add(new MainThreadSubscription() {
@Override
protected void onUnsubscribe() {
if (!realm.isClosed()) {
results.removeChangeListener(changeListener);
realm.close();
}
context = null;
clazz = null;
}
});
// Immediately call onNext with the current value, as due to Realms auto-update,
// it will be the latest value.
subscriber.onNext( results );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment