gpt4 book ai didi

android - 在RxAndroid(Observable)中添加subscription到disposable后,控制流结束,没有调用subscribe..!

转载 作者:太空宇宙 更新时间:2023-11-03 10:33:47 28 4
gpt4 key购买 nike

我正在尝试在 android 中使用 MVP、RX 和 Dagger 2。以下是代码流程,

LocalDataSource.java

@Singleton
public class LocalDataSource implements DataSource {

@Override
public Observable getServerSettings() {
return mDBHelper.createQuery(ServerSettingsEntry.TABLE_NAME,
DbUtils.getSelectAllQuery(ServerSettingsEntry.TABLE_NAME))
.mapToOne(DbUtils::getServerSettings);

Repository.java

@Singleton
public class MyRepository implements DataSource {
@Override
public Observable<ServerSettings> getServerSettings() {
return mLocalDataSource.getServerSettings().compose(RxUtils.applySchedulers());

LoginPresenter.java

public class LoginPresenter implements LoginActivityContract.Presenter{
@Override
public void checkServerDbSynced() {
mCompositeDisposable.clear();
Disposable subscription = mRepository
.getServerSettings()
.doOnSubscribe(disposable -> {
Timber.d(" onSubscribe");
mView.showLoadingIndicator(true, "Checking Server ....");
})
.subscribe(serverSettings -> {
if (serverSettings == null) {
Timber.d("*** Server Db Synced ****" + "\n" + "*** Checking Licence Key **** ");
checkLicenceKey();
} else {
Timber.d("*** Server Db Not Synced *** " + " \n" + "*** Opening Login Dialog ****");
mView.showLoadingIndicator(false, "Db Not Synced ....");
mView.openLoginDialog();
}
},
throwable -> {
mView.showErrorMessage(throwable.getLocalizedMessage());
});
mCompositeDisposable.add(subscription);
}
}



@Override
public void subscribe() {
checkServerDbSynced();
}

@Override
public void unSubscribe() {
mCompositeDisposable.clear();
}

问题是没有调用 subscribe 中的语句。调试时,我注意到在这行 composite disposable.add(subscription) 之后;控制流程结束了……请帮忙……!!

编辑

public static ServerSettings getServerSettings(@NonNull Cursor cursor) {
ServerSettings s = new ServerSettings();
s.setAndroidId("1234564453453463dfg");
s.setDeviceId("tythyerju99");
s.setIpAddress("6373792092.48949");
s.setLicenceKey("fhfhdid");
s.setExpiryDate("hshsh8ehd8");
s.setId(2);
Timber.d(" *** " +s.getExpiryDate()+" ****");
return s;
}

最佳答案

您的 doOnSubscribe 在执行 getServerSettings() 的同一线程上执行属于 Schedulers.io() 的内容。因此,您的 mView.showLoadingIndicator(true, "Checking Server ....") 正在尝试从主线程更新 UI(这是不允许的),因此此错误终止了您的 可观察的 执行 - 您可能会在 mView.showErrorMessage(throwable.getLocalizedMessage()) 调用中看到它,但我不知道您在那里具体做什么。

您应该做的是从您的 doOnSubscribe 向主线程发送一条消息,如下所示:

...
.doOnSubscribe(disposable -> {
Timber.d(" onSubscribe");
new Handler(Looper.getMainLooper()).post(() -> {
mView.showLoadingIndicator(true, "Checking Server ....");
});
})
...

更新

经过讨论,问题出在LocalDataSourcegetServerSettings方法上。具体来说,在 mapToOne 的使用中。当结果集为空时,就没有任何东西可以发出,这就是为什么 subscribe 消费者方法没有被调用的原因。为确保它不会发生,请使用 maptoOneOrDefault,如果结果集为空,它会发出指定的默认值。

关于android - 在RxAndroid(Observable)中添加subscription到disposable后,控制流结束,没有调用subscribe..!,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51820417/

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