与网络连接有关的性能问题

时间:2016-11-06 16:09:14

标签: codenameone

第一屏:

@Override
protected void postMain(Form f) {
    for (Map<String, Object> element : connectionGroup.responses) {
        String id = (String) element.get("id");
        String tableName = (String) element.get("name");
        String tableImg = (String) element.get("tablelogo");
        String clubcharterdate = (String) element.get("clubcharterdate");
        String total_members = (String) element.get("total_members");

        Container singleRowContainerr = new Container(new BoxLayout(BoxLayout.Y_AXIS));

        Container childContainer = new Container(new BorderLayout());
        singleRowContainerr.add(childContainer);

        Label shadow = new Label(" ");
        singleRowContainerr.add(shadow);

        TextArea nameLabel = new TextArea(tableName.toUpperCase());

        Label charterDate = new Label("Charter date: " + clubcharterdate);

        Label totalno = new Label("No. of member: " + total_members);
        Container dataContainer = BoxLayout.encloseY((nameLabel), charterDate, totalno);

        childContainer.add(BorderLayout.CENTER, FlowLayout.encloseMiddle(dataContainer));

        Label tableIcon = new Label();
        AllUrl allUrl = new AllUrl();
        String name = tableImg.substring(0, tableImg.lastIndexOf("."));
        Image a = URLImage.createToStorage(placeholderForTable, "tablelogo" + name, allUrl.minuteTableImgUrl + name + ".png", URLImage.RESIZE_SCALE_TO_FILL);
        tableIcon.setIcon(a);
        childContainer.add(BorderLayout.WEST, tableIcon);

        Button hidee = new Button();
        singleRowContainerr.add(hidee);
        hidee.setHidden(true);
        singleRowContainerr.setLeadComponent(hidee);
        wrapContainerSingleTable.add(singleRowContainerr);

        hidee.addActionListener((e) -> {
            detailId = hidee.getName();
            detailTableName = hidee.getUIID();
            showForm("Detail", null);
        });
    }
}

第二屏:

protected void postDetail(Form f) {
    f.setLayout(new BoxLayout(BoxLayout.Y_AXIS));

    Container memberContainer = new Container(new BoxLayout(BoxLayout.Y_AXIS));
    f.add(memberContainer);

    for (int i = 0; i < dc.response.size(); i++) {
        HashMap hm = (HashMap) dc.response.get(i);
        //members lists
        ArrayList<Map<String, Object>> membersResponse = (ArrayList) hm.get("members");
        for (Map<String, Object> element : membersResponse) {
            String profile_img = (String) element.get("profile_img");
             Image img = URLImage.createToStorage(placeholder, "memberImage" + profile_img, au.profileImgUrl + "sm_" + profile_img, URLImage.RESIZE_SCALE);
             Label icon = new Label(img);
             f.add(icon);
        }
    }
}

当在加载主页面中的所有图像之前从主页面(第一屏幕)转到详细页面(第二屏幕)时,在加载所有主屏幕图像之前不加载细节页面图像。通过查看输出中的结果我知道这一点。 例如:虽然我已经在第二个屏幕,但只有在加载了第一个屏幕的所有链接后,才能看到第二个屏幕的链接。

WARNING: Apple will no longer accept http URL connections from applications you tried to connect to http://roundtablenepal.org.np//uploads/mobile/12.png to learn more check out https://www.codenameone.com/blog/ios-http-urls.html
WARNING: Apple will no longer accept http URL connections from applications you tried to connect to http://roundtablenepal.org.np//uploads/mobile/RT.png to learn more check out https://www.codenameone.com/blog/ios-http-urls.html
WARNING: Apple will no longer accept http URL connections from applications you tried to connect to http://roundtablenepal.org.np//uploads/mobile/RT1.png to learn more check out https://www.codenameone.com/blog/ios-http-urls.html
// and all the image links of 1st screen one by one,
// then only the link of 2nd screen are seen and the page load 
//as soon as 2nd screen link starts appearing

然后在输出中只能看到详细页面中图像的链接。它最初使应用程序变慢。一旦将所有图像保存到缓存中,就没有问题。我在真实设备中也有同样的问题。第一次使用该应用程序的每个人都说app很慢。在codenameone中,当我从其他屏幕进入一个屏幕时,以前页面的所有进程和网络都被取消并且当前屏幕的新进程/网络被加载了吗?怎么做?

1 个答案:

答案 0 :(得分:0)

URLImage简单而简单,它只知道图像正在显示,因此可以开始获取它。它不了解您所处的形式或您是否不再需要图像。

我知道有些开发人员会使用像关闭网络管理器这样的大刀技巧,但这可能适用于损坏的一半下载数据和仅在设备上发生的奇怪边缘情况......

更合适的解决方案是在诸如此类的复杂情况下避免URLImage并使用了解您的应用程序的自定义代码。这就是为什么我们有ConnectionRequest中下载图像的方法,您可以使用这些方法,只有在上一个图像下载完成后才添加下一个图像。这很有用,因为它可以让您控制并行下载图像的过程。

请注意,您还可以通过调用:

来增加init(Object)方法中的网络线程数
NetworkManager.getInstance().updateThreadCount(2);