codenameone - Codenameone - 何时将 callSerally 与对话框一起使用?
问题描述
在此处的答案和 Codenameone Developer Guide 的第 7.2.1 节中,建议使用 callSerially 调用对话框。我认为这意味着而不是这个:
dlg.showDialog();
我们应该使用:
Display.getInstance().callSerially(() -> {
dlg.showDialog();
});
但我注意到在 Codenameone 代码的各个其他部分中,Dialog.shows() 没有包含在 callSerially 中,例如 ConnectionRequest.java、NetworkManager.java 和 XFClient.java。那些应该按照建议使用 callSerally 吗?如果不是,那么决定何时将 callSerially 用于对话框以及何时不使用的标准是什么?
背景:我问这个问题是因为在实现了这里讨论的网络重试逻辑之后, 我的用户遇到了一些间歇性(并且到目前为止不可能可靠地复制)应用程序锁定,我怀疑这可能与我的“是/否”对话框和XFClient 中可能同时发生的自定义网络问题对话框(见下文)。我想知道我是否应该始终对这些对话框使用 callSerial 。
protected void setupConnection(ConnectionRequest req) {
if (timeout > 0) {
req.setTimeout(timeout);
}
if (readTimeout > 0) {
req.setReadTimeout(readTimeout);
}
// remainder is custom code I added..
if (silentRetryCount > 0) {
req.setSilentRetryCount(silentRetryCount);
}
req.addExceptionListener(evt -> {
if (evt != null) {
if (req.getSilentRetryCount() > 0) {
// silentRetryCount--;
req.setSilentRetryCount(req.getSilentRetryCount() - 1);
// NetworkManager.getInstance().resetAPN(); // private, not sure if we need this?
req.retry();
return;
}
Exception exc = evt.getError();
Log.e(exc);
if (Display.isInitialized() && !Display.getInstance().isMinimized()
// && Dialog.show("Exception", exc.toString() + ": for URL " + url + "\n" + exc.getMessage(), "Retry", "Cancel")) {
&& Dialog.show("Network Issue", "Hopefully it is just a bump in the road. Suggest you retry...", "Retry", "Cancel")) {
req.retry();
}
// note: ConnectionRequest.handleException has an else block here setting retrying= false and killed=true
}
});
}
注意:添加的代码仿照 ConnectionRequest.java 中的 handleException 方法。我不知道如何添加 resetAPN 和 else 块,所以把它们排除在外。不确定这是不是一个错误?
任何帮助表示赞赏。
解决方案
callSerially
for 对话框应该在两种情况下使用:
- 您已离开 EDT - 这是必需的
- 您在一个事件链中并希望刷新当前事件。这是一个复杂的边缘情况。我不会进入它,因为它不适用于这种情况
最初,当实现网络管理器逻辑时,我们编写了重试代码。当时我们没有注意到 EDT 违规行为,也没有表现得那么明显。不幸的是,与许多此类错误一样,现在很难摆脱它。
还有其他解决方案,但更好的解决方案之一是全局使用网络错误处理程序,这更接近您想要的。这个问题涵盖了它的当前用法:区分服务器端错误和连接问题
推荐阅读
- python - 在 Pandas 中排列字符串 Month 行的最简单方法是什么
- excel - Excel 问题 - 土耳其货币的自定义格式
- django - Django rest +vue.js excel文件下载损坏
- machine-learning - 如何找到一堆词簇的特征?
- oracle - 将光标值调用到异常中
- python - 子文件夹在 python 中不能作为包工作
- android - 如何从 livedata 获取列表?
- sql-server - 使用部分三部分名称(如 dbname..tablename)时如何使 SQL 查询失败
- ruby-on-rails - 如何在rails中选择不同的行
- php - 刀片视图未从 laravel 中的控制器获取数据