- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试在 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 ....");
});
})
...
更新
经过讨论,问题出在LocalDataSource
和getServerSettings
方法上。具体来说,在 mapToOne
的使用中。当结果集为空时,就没有任何东西可以发出,这就是为什么 subscribe
消费者方法没有被调用的原因。为确保它不会发生,请使用 maptoOneOrDefault
,如果结果集为空,它会发出指定的默认值。
关于android - 在RxAndroid(Observable)中添加subscription到disposable后,控制流结束,没有调用subscribe..!,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51820417/
我在 dispose 和 finalize 之间犹豫不决。这是我的示例代码: public class Car:IDisposable { public string name
我有一个 Activity,我在其中创建并订阅 Single 类的多个实例(每个实例都在单独的后台线程中执行一些工作)。对于每个订阅,我将创建的 Disposable 实例添加到 CompositeD
我有一个服务、一个存储库和一个 DbContext。存储库拥有 DbContext,服务拥有存储库。 我的存储库应该实现 IDisposable 吗?如果是这样,我的服务是否也应该实现 IDispos
我有一个基于字符串列表处理文件流的应用程序,字符串可以是磁盘上的文件,也可以是 Zip 文件中的文件。为了清理代码,我想重构打开文件的过程。 我创建了一个返回文件内容 Stream 的方法,但是因为流
有没有一种方法可以从 Dispose 方法中取消处置,换句话说,在调用 A.Dispose() 时不处置 A。 更新我没有很好地解释,因为我想不出一个很好的方式来表达它。我拥有的是一个在单独线程中运行
如以下示例实现即 https://learn.microsoft.com/en-us/dotnet/standard/garbage-collection/implementing-dispose有一
我开始用 FXCop 来处理我的一些程序集,并注意到一些有趣的事情,我正试图全神贯注。 "Types that declare disposable members should also imple
当我创建表单时,自动生成的代码不包含重写的 Dispose 方法。这是否意味着没有为表单中的所有控件调用 Dispose? 最佳答案 当您在表单上调用 Dispose 时,它会为其 Controls
在我的 WPF 应用程序代码中,我收到以下警告: CA1001 Types that own disposable fields should be disposable Implement IDis
每当我读到 Close() 和 Dispose() 时,我都会看到很多只使用 Using Block 的引用资料,但我还没有找到如何在 WebMatrix C# Razor 语法中使用 Using B
我是 c# 和 VSTO 的新手。 我有一个 Excel 插件,我在其中创建了一个功能区和一些可以执行各种操作的新按钮。 我在 base.Dispose 中收到 NullReferenceExcept
我在 FCL 代码中发现了一个不寻常的样本。 这是 System.IO.BinaryReader 中的方法: protected virtual void Dispose(bool dispo
我正在尝试遵循以下教程并将其从 C# 转换为 Vb.net http://www.asp.net/mvc/overview/older-versions/getting-started-with-ef
我有一个 LINQ 对象,其中添加了一个附加方法。该类没有一次性属性或方法,但 FxCop 引发错误“拥有一次性字段的类型应该是一次性的”并引用该类。 到目前为止我已经减少了代码,但仍然收到错误: p
因此,默认的处理模式实现如下所示: class SomeClass : IDisposable { // Flag: Has Dispose already been called? bo
我在 WinForms 应用程序中使用 RxFramework。我正在尝试运行 Observable 异步并使用 CancellationDisposable 在用户单击按钮时取消操作。但它不工作!
有很多从 .NET 中的函数返回 IDisposable 对象数组或列表的示例。例如,Process.GetProcesses()。 如果我调用该方法,我是否有责任在遍历数组的所有成员时对它们进行 D
如果我用 C# 编写一个实现 IDisposable 的类,为什么我不能简单地实现 public void Dispose(){ ... } 处理释放任何非托管资源? 是 protected virt
LibGDX - 是否应该先执行 texture.dispose() 然后再执行 batch.dispose()?还是没有什么不同? 我有以下简单的代码。我的问题是 texture.dispose()
当应用程序收到来自服务的调用时,它会为每个调用打开一个表单。用户必须在每个窗口中进行输入并关闭它。为了让用户的工作顺利进行,我试图在显示下一个窗口时重新激活用户正在处理的窗口。 执行此操作的方法如下:
我是一名优秀的程序员,十分优秀!