Skip to content

Instantly share code, notes, and snippets.

@giorgiopagnoni
Last active August 29, 2015 14:21
Show Gist options
  • Save giorgiopagnoni/8eb5337e16123a905be2 to your computer and use it in GitHub Desktop.
Save giorgiopagnoni/8eb5337e16123a905be2 to your computer and use it in GitHub Desktop.
Remove recursively all the children from a view in Appcelerator Titanium (Android)
/*
* Does this really help solve some memory leaks...?
*/
Alloy.Globals.removeChildren = function(view) {
if (view) {
if (view.length) {
// it's an array of views (eg. the views of a scrollableView)
for (var i = 0; i < view.length; i++) {
Alloy.Globals.removeChildren(view[i]);
}
} else {
if (view.children) {
// https://developer.appcelerator.com/question/78311/removing-all-child-objects-from-a-view
var removeData = [];
for (var i = view.children.length; i > 0; i--) {
removeData.push(view.children[i - 1]);
}
for (var i = 0; i < removeData.length; i++) {
view.remove(removeData[i]);
Alloy.Globals.removeChildren(removeData[i]);
}
removeData = null;
}
// handle special cases
switch(view.apiName) {
case 'Ti.UI.ScrollableView':
// useless because the views are automatically removed (?) from the scrollableView if the parent window is being closed
// views is always an empty array
var views = view.views;
for (var i = 0; i < views.length; i++) {
Alloy.Globals.removeChildren(views[i]);
}
break;
case 'Ti.UI.TableView':
// http://developer.appcelerator.com/question/132357/memory-leaks-on-android-but-not-on-ios---very-simple-test-case
view.setData([]);
break;
case 'Ti.UI.ImageView':
// http://developer.appcelerator.com/question/132357/memory-leaks-on-android-but-not-on-ios---very-simple-test-case
view.image = '';
break;
}
}
view = null;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment