问题
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
$.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.
When I want to remove the ScrollableView, I run the cleanup function on every View, and it's children.
Should I run the cleanup function on all the ScrollableView views?
Should I null all the ScrollableView views?
Should I remove all the ScrollableView views?
Should I run the cleanup function on all the View children?
Should I null all the View children?
Should I remove all the View children?
UPDATE
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;
};
Anyway, if my fields have an event listener added like 'change' or 'click', I must remove it in cleanup function, right?
回答1:
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.
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).
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.
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.
You can read more about memory management in this article on TiDev which is still very relevant.
In your updated question you set all the sub-views in the data
object. null
ing the data object will also drop all references to the views, so that should be enough.
来源:https://stackoverflow.com/questions/37696616/its-necessary-to-remove-views-in-order-to-cleanup-the-alloy-controller-memory