Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-8620] iOS: Ti.UI.View is a live reference, not copy

GitHub Issuen/a
TypeBug
PriorityMedium
StatusClosed
ResolutionFixed
Resolution Date2012-04-30T16:45:24.000+0000
Affected Version/sRelease 1.7.5, Release 1.8.2
Fix Version/sRelease 2.1.0, Sprint 2012-09 Core
ComponentsiOS
LabelsSupportTeam, core, module_view, qe-port
ReporterSuresh Sharma
AssigneeStephen Tramer
Created2012-04-05T03:00:15.000+0000
Updated2012-06-23T15:33:39.000+0000

Description

BUG

---- Currently, when Ti.UI.View.children is queried, the live copy of children is returned, meaning that if it is modified in-place during a loop (which might be a common operation) there are some issues. Even worse, it follows copy semantics before a view is put into the full hierarchy, and reference semantics after.

INITIAL DESCRIPTION

----

Feature description

Provide an easy way to clean the contents of a view/window, like a magic "remove all children" that will remove them in the right order.

Current behavior

Customer is trying to do this:
for(var i=0;i<parent.children.length;i++)
{
   parent.remove(parent.children[i]);
}
which causes a crash in the app because they are removing stuff without any order. Maybe we should provide a tree with all the dependencies in the view, so they can be properly removed.

Comments

  1. Ivan Skugor 2012-04-09

    Here is my solution: https://gist.github.com/1779850 (and explanation why it does not work with "customer's way")
  2. Stephen Tramer 2012-04-27

    Testing

    ---- * Run the following code sample - no interactions necessary. Note that the long delays are necessary due to inefficiencies in the rendering system. Also note that there may be occasional errors as the system tries to remove the same view more than once - this is because remove() is asynchronous. This is not a fatal error.
       var win = Ti.UI.createWindow();
       
       function construct() {
       	for (var i=0; i < 30; i++) {
       		for (var j=0; j < 30; j++) {
       			var bgcolor;
       			switch (i % 3) {
       				case 0:
       					bgcolor = 'red';
       					break;
       				case 1:
       					bgcolor = 'blue';
       					break;
       				case 2:
       					bgcolor = 'green';
       					break;
       			}
       			var view = Ti.UI.createView({
       				top:(j*10),
       				left:(i*10),
       				width:10,
       				height:10,
       				backgroundColor:bgcolor
       			});
       			win.add(view);
       			Ti.API.info(win.children.length);
       		}
       	}
       	setTimeout(destruct, 3000);
       }
       
       function destruct() {
       	while (win.children != null && 
       			win.children != undefined && 
       			win.children.length > 0) 
       	{
       		Ti.API.info(win.children.length);
       		win.remove(win.children[0]);
       	}
       	setTimeout(construct, 3000);
       }
       
       win.open();
       construct();
       
    There is a separate issue with Ti.UI.View.children being undefined when the view does not have children. This may be a problem on Android as well, and we are investigating the parity problem.
  3. Michael Pettiford 2012-06-23

    Closing issue Tested with Ti Studio build 2.1.0.201206211609 Ti Mobile SDK 2.1.0.v20120622174154 hash rdc9dfbe5 OSX Lion 10.7.3 iPhone 4S OS 5.1 Verified expected behavior is shown

JSON Source