Hello. Recently began to deal with RxJava. Something hard is still being given ... There is a server on which there are private chats (one-on-one) and group chats. Through the Rest request to the server, you need to extract the following information from there: All existing chats and dialogues:

Observable<List<Chat>> chats = apiService.getChats(); Observable<List<Dialog>> dialogs = apiService.getDialogs(); 

The Chat and Dialog objects contain variables:

 int unreadMessagesCount (количество непрочитанных сообщений); int id (по этому id запрашивается список сообщений из чата) 

Requests for message list

 Observable<List<Message>> dialogMessages = apiService.getDialogMessages(String id); Observable<List<Message>> chatMessages = apiService.getChatMessages(String id); 

How, in this case, is it more competent to create requests using RxJava to poll all chats and dialogues for new messages and then get these messages in one list?

    1 answer 1

    For example, something like this:

    1. We get an array of chat.
    2. Convert an array of these into a queue of Chat objects.
    3. We get the details of each.
    4. The result is converted back to an array :.
     apiService.getChats() .from(Observable::from) .flatMap(chat -> apiService.getChatMessages(chat.id)) .toList() .subscribe(System::out); 

    If without lambdas, then from(Observable::from) can be rewritten with such a terrible construct:

     .flatMap(new Func1<List<Chat>, Observable<Chat>>() { @Override public Observable<Chat> call(List<Chat> chats) { return Observable.from(chats); } }) 
    • Apparently he didn’t understand something ... Got "cannot resolve method 'from (<method reference>)'" - Nikolay Medvedev
    • @Nikolai Medvedev, maybe your lambda isn’t working. See addition to the answer - YurySPb