【问题标题】:Codenameone - When to use callSerially with Dialogs?Codenameone - 何时将 callSerally 与对话框一起使用?
【发布时间】:2020-05-29 12:37:00
【问题描述】:

在答案 here 和 Codenameone 开发人员指南的第 7.2.1 节中,建议使用 callSerially 调用对话框。我认为这意味着而不是这个:

                    dlg.showDialog();

我们应该使用:

                Display.getInstance().callSerially(() -> {
                    dlg.showDialog();
                });

但我注意到在 Codenameone 代码的各个其他部分中,Dialog.shows() 没有包含在 callSerially 中,例如 ConnectionRequest.java、NetworkManager.java 和 XFClient.java。那些应该按照建议使用 callSerally 吗?如果不是,那么决定何时将 callSerially 用于 Dialogs 以及何时不使用的标准是什么?

背景:我问这个问题是因为在实施了讨论过的网络重试逻辑here 之后,我的用户遇到了一些间歇性(并且到目前为止无法可靠地复制)应用程序锁定,我怀疑这可能与我之间的冲突有关XFClient 中的 Yes/No 对话框和自定义网络问题对话框(见下文)可能同时发生。我想知道我是否应该始终对这些对话框使用 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 块,所以把它们排除在外。不知道是不是搞错了?

任何帮助表示赞赏。

【问题讨论】:

    标签: codenameone


    【解决方案1】:

    callSerially 用于对话框应该在两种情况下使用:

    • 您已离开 EDT - 这是必需的
    • 您处于事件链中并希望刷新当前事件。这是一个复杂的边缘情况。因为不适用于本案,所以我就不赘述了

    最初在实现网络管理器逻辑时,我们编写了重试代码。当时我们没有注意到 EDT 违规行为,也没有表现得那么明显。不幸的是,与许多此类错误一样,现在很难摆脱它。

    对此还有其他解决方案,但更好的解决方案之一是全局使用网络错误处理程序,这更接近您想要的。这个问题涵盖了它的当前用法:Distinguish between server-side errors and connection problems

    【讨论】:

    • 谢谢。我尝试删除从 EDT 调用的任何对话框,或将它们切换为 callSerially 或 callSeriallyAndWait。解决问题的手指交叉!
    猜你喜欢
    • 2022-09-30
    • 2018-02-17
    • 2012-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-07
    相关资源
    最近更新 更多