gpt4 book ai didi

java - 使用 RXJava 2 异步读/写 Realm

转载 作者:行者123 更新时间:2023-11-29 19:06:21 25 4
gpt4 key购买 nike

我第一次使用 RXJava 2 实现异步操作

目标:

获取json来自带有库的服务器的数据 Retrofit2 .如果成功,则将数据写入Realm并在记录后立即取回数据并发送到 RecyclerView 的适配器.

于是,我以这种方式实现了这一切:

private void fetchChatsFromNetwork(int count, AccessDataModel accessDataModel) {

String accessToken = accessDataModel.getAccessToken();

MyApplication.getRestApi().getChats(count, accessToken, Constants.api_version)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribeWith(new DisposableSubscriber<ChatsModel>() {
@Override
public void onNext(ChatsModel chatsModel) {
if (chatsRepository.hasData()) {

chatsRepository.updateChatsData(chatsModel)
.subscribe(new DisposableObserver<ChatsModel>() {
@Override
public void onNext(ChatsModel localChatsModel) {
Log.d(TAG, "DO, onSuccess updated!");
iGetChatsCallback.onGetChatsSuccess(localChatsModel);
}

@Override
public void onError(Throwable e) {
Log.d(TAG, "DO, onError when update!");
iGetChatsCallback.onGetChatsError(e.getMessage());
}

@Override
public void onComplete() {
dispose();
Log.d(TAG, "DO, onComplete!");
}
});

} else {
chatsRepository.insertChatsData(chatsModel)
.subscribe(new DisposableObserver<ChatsModel>() {
@Override
public void onNext(ChatsModel localChatsModel) {
iGetChatsCallback.onGetChatsSuccess(localChatsModel);
Log.d(TAG, "DO, onSuccess inserted!");
}

@Override
public void onError(Throwable e) {
iGetChatsCallback.onGetChatsError(e.getMessage());
Log.d(TAG, "DO, onError when inserting!");
}

@Override
public void onComplete() {
dispose();
Log.d(TAG, "DO, onComplete!");
}
});
}
}

@Override
public void onError(Throwable t) {
Log.d(TAG, "onError" + t.getMessage());
}

@Override
public void onComplete() {
Log.d(TAG, "onComplete");
}
});
}

我把数据写在Realm中在onNext()订户方式MyApplication.getRestApi().GetChats()

这是入口代码:

public Observable<ChatsModel> updateChatsData(final ChatsModel chatsModel) {

return Observable.create(new ObservableOnSubscribe<ChatsModel>() {
@Override
public void subscribe(ObservableEmitter<ChatsModel> e) throws Exception {
if (chatsModel != null) {
realm.executeTransactionAsync(
realm -> realm.copyToRealmOrUpdate(chatsModel),
() -> {
Log.d(LOG_TAG, "Data success updated!");
ChatsModel localChatsModel = getAllChatsData();
e.onNext(localChatsModel);
e.onComplete();
},
error -> {
Log.d(LOG_TAG, "Update data failed!");
e.onError(error);
});
}

}
});

}

updateChatsData()异步写入并在另一个类中声明。

如您所见,fetchChatsFromNetwork()方法写的比较繁琐我觉得还是这样

问题:

我做的对不对,如果不对,那怎么会是对的?

最佳答案

private void fetchChatsFromNetwork(int count, AccessDataModel accessDataModel) {    
String accessToken = accessDataModel.getAccessToken();
Single<ChatsModel> chats = MyApplication.getRestApi().getChats(count, accessToken, Constants.api_version);
chats.doOnNext((chats) -> {
chatsRepository.insertOrUpdate(chats);
}).subscribeOn(Schedulers.io())
.subscribe();
}

public void updateChatsData(final ChatsModel chatsModel) {
try(Realm realm = Realm.getDefaultInstance()) {
realm.executeTransaction(r -> {
r.insertOrUpdate(chatsModel);
});
}
}

public Flowable<List<ChatsModel>> getAllChatsData(Realm realm) {
RealmQuery<ChatsModel> query = realm.where(ChatsModel.class);
if(realm.isAutoRefresh()) {
return query.findAllAsync().asFlowable().filter(RealmResults::isLoaded);
} else {
return Flowable.just(query.findAll());
}
}

关于java - 使用 RXJava 2 异步读/写 Realm,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47078244/

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