Codenameone-何时将callSerially与对话框一起使用?

问题描述 投票:1回答:1

在答案here和《 Codenameone开发人员指南》的7.2.1节中,建议应使用callSerially调用对话框。我认为这意味着代替:

                    dlg.showDialog();

我们应该使用:

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

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

[背景:我问这个问题,因为在讨论了here之后,实现了网络重试逻辑后,我的用户出现了一些间歇性的(目前为止无法可靠地复制)应用锁定,我怀疑这可能与我的是之间存在冲突XFClient中的/ No对话框和自定义网络问题对话框(请参见下文)可能同时发生。我想知道是否应该对这些对话框始终使用callSerially。

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,否则将其阻塞。不确定这是否是一个错误?

感谢任何帮助。

codenameone
1个回答
0
投票

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

  • 您已退出EDT-这是必需的
  • 您在事件链中,希望刷新当前事件。这是一个复杂的情况。我不会涉及它,因为它不适用于这种情况

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

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

© www.soinside.com 2019 - 2024. All rights reserved.