Jude Fernandes Jude Fernandes - 9 months ago 32
Android Question

RxJava Retrofit call not being made

I am trying to sync all local data with the data on the server so i am fetching all local data and for anything other than local data i am making a call to the server to fetch the rest of the data.

public Observable<List<Chat>> getChats(int userId) {

Log.i("chatRequest", TAG);

return Observable.merge(chatDiskData.getChats(), chatDiskData.getChats().flatMap(new Function<RealmResults<Chat>, ObservableSource<List<Chat>>>() {
@Override
public ObservableSource<List<Chat>> apply(@NonNull RealmResults<Chat> chats) throws Exception {

Log.i("chatRequest", TAG + " inside");

ArrayList<String> chatIds = new ArrayList<>();
HashMap<String, Long> chatsTimeStamp = new HashMap<>();

for (int i = 0; i < chats.size(); i++) {
String chatId = chats.get(i).getChat_id();
chatIds.add(chatId);

long timestamp = chatDiskData.lastMessage(chatId);
chatsTimeStamp.put(chatId, timestamp);
}

return chatCloudData.getChats(userId, chatIds, chatsTimeStamp).subscribeOn(Schedulers.io())
.doOnNext(chats1 -> Log.i("chatRequest", TAG + " inside inside"));
}
}));
}


ChatCloudData

public Observable<List<Chat>> getChats(int userId, ArrayList<String>
chatIds, HashMap<String, Long> chats) {

Log.i("chatRequest", TAG);

ChatsRequest chatsRequest = new ChatsRequest.Builder(userId)
.chatIDs(chatIds)
.chats(chats)
.build();

return apiService.getChats(chatsRequest);
}


Endpoint

@POST("Chat/sync")
Observable<List<Chat>> getChats(@Body ChatsRequest chatsRequest);


The output it as follows

03-11 22:44:45.430 31540-31540/com.project I/chatRequest: com.project.ChatFragment
03-11 22:44:45.430 31540-31540/com.project I/chatRequest: com.project.Repository.ChatRepository
03-11 22:44:45.430 31540-31540/com.project I/chatRequest: com.project.Repository.ChatDiskData getChats
03-11 22:44:45.436 31540-31540/com.project I/chatRequest: com.project.Repository.ChatDiskData getChats
03-11 22:44:45.442 31540-31815/com.project I/chatRequest: com.project.Repository.ChatRepository inside


Why isn't the getChats method of cloudData being called?

Answer Source

I think you should precise on that scheduler you want to emit your value :

return chatCloudData.getChats(userId, chatIds, chatsTimeStamp).subscribeOn(Schedulers.io()).observeOn(relevantScheduler)
                    .doOnNext(chats1 -> Log.i("chatRequest", TAG + "  inside  inside"));

EDIT:

public Observable<List<Chat>> getChats(int userId) {

    Log.i("chatRequest", TAG);

    return Observable.merge(chatDiskData.getChats(), chatDiskData.getChats().flatMap(new Function<RealmResults<Chat>, ObservableSource<List<Chat>>>() {
        @Override
        public ObservableSource<List<Chat>> apply(@NonNull RealmResults<Chat> chats) throws Exception {

            Log.i("chatRequest", TAG + "  inside");

            ArrayList<String> chatIds = new ArrayList<>();
            HashMap<String, Long> chatsTimeStamp = new HashMap<>();

            for (int i = 0; i < chats.size(); i++) {
                String chatId = chats.get(i).getChat_id();
                chatIds.add(chatId);

                long timestamp = chatDiskData.lastMessage(chatId);
                chatsTimeStamp.put(chatId, timestamp);
            }

            return Observable.fromCallable(new Callable<List<Chat>>() {
                @Override
                public List<Chat> call() throws Exception {
                    return chatCloudData.getChats(userId, chatIds, chatsTimeStamp).subscribeOn(Schedulers.io())
                    .doOnNext(chats1 -> Log.i("chatRequest", TAG + "  inside  inside"));
                }
            });
        }
    }));
}

ChatCloudData

public List<Chat> getChats(int userId, ArrayList<String> 
    chatIds, HashMap<String, Long> chats) {

    Log.i("chatRequest", TAG);

    ChatsRequest chatsRequest = new ChatsRequest.Builder(userId)
            .chatIDs(chatIds)
            .chats(chats)
            .build();

    return apiService.getChats(chatsRequest);
}

Endpoint

@POST("Chat/sync")
    List<Chat> getChats(@Body ChatsRequest chatsRequest);

Hope this helps.

Sorry for my english.