RxJava 2 / Retrofit 2未拨打电话

时间:2017-03-11 17:22:16

标签: java android rx-java retrofit2 rx-java2

我正在尝试将所有本地数据与服务器上的数据同步,因此我将获取所有本地数据,除了本地数据之外,我正在调用服务器以获取其余数据。

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);
}

端点

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

输出如下

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

为什么没有调用cloudData的getChats方法?

2 个答案:

答案 0 :(得分:1)

return Observable.merge(chatDiskData.getChats().observeOn(AndroidSchedulers.mainThread()),
            chatDiskData.getChats().observeOn(AndroidSchedulers.mainThread())
                    .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"));
                        }
                    }));

.observeOn(AndroidSchedulers.mainThread()是我使用领域的重要部分,它是在不同的线程上获取数据。

答案 1 :(得分:0)

我认为你应该准确地确定你要发布价值的调度程序:

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

编辑:

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);
}

端点

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

希望这有帮助。

抱歉我的英文。