gpt4 book ai didi

java - 未进行 RxJava 2/Retrofit 2 调用

转载 作者:行者123 更新时间:2023-12-02 13:41:12 25 4
gpt4 key购买 nike

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

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

聊天云数据

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方法?

最佳答案

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() 是重要的部分,因为我正在使用领域并且它在不同的线程上获取数据。

关于java - 未进行 RxJava 2/Retrofit 2 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42738278/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com