有必要删除视图吗?为了清理合金控制器(内存/性能) [英] It's necessary to remove views? In order to cleanup the Alloy controller (memory/performance)

查看:126
本文介绍了有必要删除视图吗?为了清理合金控制器(内存/性能)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说我有一个带有3个视图(窗体)的ScrollableView,这些窗体视图至少有10个字段,请看一下这个示例.

Lets say that I've a ScrollableView with 3 Views (forms), those form views have at least 10 fields, take a look at this exemple.

index.js

$.content.add(Alloy.createController('scrollable').getView());

scrollable.js

scrollable.js

$.scrollableView.addView(Alloy.createController('form',{
    fields:[
        {label:'field 1',type:'text'},
        {label:'field 1',type:'date',value:'2016-06-08'},
        ...
    ]
}).getView());

$.scrollableView.cleanup = function() {

    $.destroy();

    $.off();

    for(var i = parseInt($.scrollableView.views.length); i > 0; i--) if($.scrollableView.views[i-1]) {

        if($.scrollableView.views[i-1].cleanup) $.scrollableView.views[i-1].cleanup();
        $.scrollableView.views[i-1] = null;
        $.scrollableView.removeView($.scrollableView.views[i-1]);
    }   

    $ = args = null;
};

form.js

for(var i in args.fields) $.form.add(Alloy.createController('field',args.fields[i]).getView());

$.form.cleanup = function() {

    $.destroy();

    $.off();

    for(var i in $.form.children) {

        if($.form.children[i].cleanup) $.form.children[i].cleanup();
        $.form.children[i] = null;
    }

    $.form.removeAllChildren();

    $ = args = null;
};

当我清理所有控制器时,我仍然不知道该怎么做.

When I'm cleaning up all the controllers, I still don't understand what it's necessary to do.

当我想删除ScrollableView时,我在每个View及其子控件上运行cleanup函数.

When I want to remove the ScrollableView, I run the cleanup function on every View, and it's children.

  • 我应该在所有ScrollableView视图上运行清理功能吗?

  • Should I run the cleanup function on all the ScrollableView views?

是否应该将所有ScrollableView视图都为空?

Should I null all the ScrollableView views?

我应该删除所有ScrollableView视图吗?

Should I remove all the ScrollableView views?

我应该在所有View子级上运行清理功能吗?

Should I run the cleanup function on all the View children?

我是否应该将所有View子级为空?

Should I null all the View children?

我应该删除所有View子级吗?

Should I remove all the View children?

更新

在这种情况下,我仍然需要清理所有字段?或将数据设置为null即可解决?

In this case, I still need to cleanup all the fields? or setting the data to null will solve that?

form.js

var args = arguments[0],
    data = {
        fields:{}
    };

for(var i in args.fields) {

    data.fields[args.fields[i].label] = Alloy.createController('field',args.fields[i]).getView();

    $.form.add(data.fields[args.fields[i].label]);
}

$.form.cleanup = function() {

    $.destroy();

    $.off();

    //this is needed?
    for(var i in data.fields) {

        if(data.fields[i].cleanup) data.fields[i].cleanup();
        data.fields[i] = null;
    }
    //this is needed?

    $ = data = args = null;
};

无论如何,如果我的字段添加了事件监听器(例如"change"或"click"),那么我必须在清除功能中将其删除,对吧?

Anyway, if my fields have an event listener added like 'change' or 'click', I must remove it in cleanup function, right?

推荐答案

无需删除所有视图,清理内存唯一需要做的就是删除最父视图,以及对其中任何内容的所有引用最父视图&对父视图的引用.

There is no need to remove all views, the only thing you need to do to clean up memory is remove the most parent view, and all references to anything within the most parent view & the reference to the parent view.

因此,在您的情况下,您只需要删除ScrollableView,就可以在scrollableview中执行$.off().仅当使用数据绑定(模型/集合)时才需要$.destroy().

So in your case, you only have to remove the ScrollableView and within the scrollableview you need to do $.off(). $.destroy() is only needed if you use data-binding (models/collections).

由于您的子视图永远都没有引用(变量),因此无需删除它们.它会由Appcelerator/JavaScript自动处理,并在需要时通过垃圾回收进行清理.

Because your child views never have a reference (variable), there is no need to remove them. It is automatically handled by Appcelerator/JavaScript and will be cleaned up with garbage collection when the time comes.

注意:删除视图后不会直接进行垃圾收集,因此可能仍会增加内存使用量. JavaScript和本机平台都具有自己的垃圾回收.

note: Garbage collection doesn't happen directly after you remove the views, so you might still have increased memory usage. Both JavaScript and the native platform have their own garbage collection.

您可以在有关TiDev的文章中了解更多有关内存管理的信息仍然非常重要.

You can read more about memory management in this article on TiDev which is still very relevant.

在更新的问题中,您设置data对象中的所有子视图. null添加数据对象也将删除所有对视图的引用,因此就足够了.

In your updated question you set all the sub-views in the data object. nulling the data object will also drop all references to the views, so that should be enough.

这篇关于有必要删除视图吗?为了清理合金控制器(内存/性能)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆