Reputation: 164
I use RxJava and Realm DB for my application. When I query the data in the Realm and call Realm.asObservable()
,The RealmResult emits 2 times.
public class JustTest extends Activity {
private static final String TAG = "TEST";
private Realm realm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
realm = Realm.getDefaultInstance();
realm.where(Group.class).findAllAsync().asObservable()
.subscribe(new Action1<RealmResults<Group>>() {
@Override
public void call(RealmResults<Group> groups) {
XLog.d(TAG, "Realm launch group result, size " + groups.size());
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
realm.close();
}
}
Log:
TEST: Realm emits group result, size 0
TEST: Realm emits group result, size 1
Upvotes: 2
Views: 931
Reputation: 20126
That is by design, as Realm emits a placeholder object immediately if you subscribe to it. If you are only interested in the first "real" result you can do something like this:
realm.where(Foo.class).findAllAsync().asObservable()
.filter(obj::isLoaded) // isLoaded is true when query is completed
.first() // Only get the first result and then complete
.subscribe(...)
This is not immediately clear from the current documentation, but we are in the process of updating the JavaDoc to explain this better: https://github.com/realm/realm-java/pull/2201
Upvotes: 5