Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-5167] iOS: Immutable objects on Window object

GitHub Issuen/a
TypeBug
PriorityLow
StatusClosed
ResolutionWon't Do
Resolution Date2020-01-09T19:22:56.000+0000
Affected Version/sn/a
Fix Version/sn/a
ComponentsiOS
Labelscore
ReporterMatthew O'Riordan
AssigneeIngo Muschenetz
Created2011-08-16T14:51:19.000+0000
Updated2020-01-09T19:22:56.000+0000

Description

When you attach an object to a Window on iOS the object is immutable, yet on Android it works fine. For example, win.myCustomObject = { id: val } works as expected. However, when you then try and change a value within that object, the changes are never made i.e. win.myCustomObject.id = newVal, the id stays the same. Code example requires two files:
var win = Ti.UI.createWindow({
  backgroundColor: '#000',
  url: 'window_object_win.js',
  layout:'vertical'
});

win.data = {
  text: 'Initialized'
};

win.open();

win.addEventListener('modifyData', function() {
  win.data.text = 'This should be updated if this object is mutable';
  win.fireEvent('dataUpdated');
});

win.addEventListener('replaceData', function() {
  win.data = { text: 'If the data has changed now then replacing the entire data object works ' };
  win.fireEvent('dataUpdated');
});
var modifyButton = Ti.UI.createButton({
  title: 'Modify window .data object',
  top: 60,
  width: 250,
  height: 80
});

var replaceButton = Ti.UI.createButton({
  title: 'Replace window .data object',
  top: 60,
  width: 250,
  height: 80
});

var label = Ti.UI.createLabel({
  color: '#FFF',
  top: 20,
  height: 200
});

Ti.UI.currentWindow.add(modifyButton);
Ti.UI.currentWindow.add(replaceButton);
Ti.UI.currentWindow.add(label);

modifyButton.addEventListener('click', function() {
  Ti.UI.currentWindow.fireEvent('modifyData');
});

replaceButton.addEventListener('click', function() {
  Ti.UI.currentWindow.fireEvent('replaceData');
});

var counter = 0;

var updateLabelFromData = function() {
  label.text = 'Text (' + counter++ + ' presses) = ' + Ti.UI.currentWindow.data.text;
};

Ti.UI.currentWindow.addEventListener('dataUpdated', updateLabelFromData);

updateLabelFromData();
To replicate this issue with this code example, load up the app, click on the bottom button (to modify values) and you will see that the value does not change. However, if you click on the button above that which replaces the entire object on the window, it works fine. On Android both buttons work as expected.

Comments

  1. Paul Dowsett 2011-08-25

    Matthew Can you see if there are any significant messages output to the log, and include them in your ticket in a code block, if you do? Many thanks
  2. Matthew O'Riordan 2011-08-26

    There aren't any messages related to this code.
  3. James David Low 2012-07-23

    I get this same issue when passing variables as part of a window AND when using Ti.App. I do something like: var someObject = {}; Ti.App.refToSomeObject = someObject; If I make changes to someObject in say a callback from an HTTP request and then another window accesses Ti.App.refToSomeObject, the changes aren't propagated.
  4. James David Low 2012-07-23

    Someone else has this same problem here: http://developer.appcelerator.com/question/65211/data-between-contexts-reference-vs-titaniumappproperties-#answer-129821
  5. James David Low 2012-07-23

    I found an issue in the support related to this: http://support.appcelerator.com/tickets/XIY-29136-551/report In case you can't see it, Nolan Wright says: "...attach properties to your window objects as you create them. the downside here is that each one is a copy so they are not the same variable - i.e., a change in one will not be reflected in the others."
  6. Neeraj Gupta 2012-07-23

    @James - Please let me know the SDK version you are using.
  7. Stephen Tramer 2012-07-24

    This is caused by long-standing critical bug TIMOB-2392, which requires a significant investment in changing the iOS platform internals.
  8. Alan Hutton 2020-01-09

    It has been decided that this issue should be closed as “Won’t do.” This issue is out of date with our current supported SDK release (7.5.2.GA as of the date of closure), and out of date with mobile OS versions. Updating, or creating code may not reproduce the issue reported, or be a valid test case. If community members feel that the issue is still valid, please create a new ticket. Please reference this closed ticket number, include SDK used, comments, and code that demonstrates/reproduces the issue.

JSON Source