I'm trying to create an observable that returns a list from a firebase query. the problem is when I call onNext to emit the Item then onComplete it stops emitting items which is after the first item, and not calling onComplete at all emits nothing. Is there a correct way to do what I'm trying to achieve? I'm very new to RxJava
still, so please excuse my ignorance. thank you in advanced for any help :)
public Observable<Message> getMessageObservable(String uid) {
currentUser = auth.getCurrentUser();
DatabaseReference db_messages = db_root.child("Messages").child(currentUser.getUid())
.child(uid);
Query messageQuery = db_messages.orderByKey().limitToLast(10);
return Observable.create(emitter -> {
messageQuery.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
String messageText = dataSnapshot.child("message").getValue().toString();
String messageId = dataSnapshot.child("MessageId").getValue().toString();
Boolean seen = dataSnapshot.child("seen").getValue(Boolean.class);
Long timestamp = dataSnapshot.child("timestamp").getValue(long.class);
String fromUser = dataSnapshot.child("from").getValue().toString();
String toUser = dataSnapshot.child("to").getValue().toString();
Message message = new Message(messageText, toUser, messageId, seen, timestamp, null, fromUser);
emitter.onNext(message);
emitter.onComplete();
}
@Override
public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
}
@Override
public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
});
}
@Override
public void getMessages(String userId) {
currentUser = auth.getCurrentUser();
Observable.just(userId)
.flatMap(this::getMessageObservable)
.toList()
.subscribe(messages -> {
chatResults.getMessagesResult(messages);
});
}
onChildAdded
you're onComplete'ing and surely it stops emitting. At the same time, by not calling onComplete you simply cannot receive your list in your subscription. – ror