Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

Data between contexts, Reference vs Titanium.App.Properties ?

Hello there,

Im was thinking about basic mechanics for data exchange between main context and (reusable) sub context. Whats your opinion/best practice on this ? Basically there are 2 variants.

1) By reference

in app.js:

var myData = [....];
function myFunction(someId){...};
var winSub = Titanium.UI.createWindow({url:'winSub.js'});
winSub.dataProvider = myData;
winSub.notifier = myFunction;
winSub.open();

in winSub.js:

// populate displays with
Titanium.UI.currentWindow.dataProvider;
// notify app.js when something happens.
Titanium.UI.currentWindow.notifier(someId);

OR

2) By name

in app.js:

var myData = [....];
Titanium.App.Properties.setList('dataProviderName', myData);
Titanium.App.addEventListener('onSubSelection', function(e){//use e.id} );
var winSub = Titanium.UI.createWindow({url:'winSub.js'});
winSub.notifyEventName = 'onSubSelection';
winSub.open();

in winSub.js:

// populate displays with
Titanium.App.Properties.getList('dataProviderName');
// notify app.js when something happens.
Titanium.App.fireEvent(Titanium.UI.currentWindow.notifyEventName, {id='someId'});

The reason i came up with option 2 is to avoid cross references between contexts and trouble with memory management, especially if the data is used in event-handlers. (If i understand correctly, events between contexts never transfer references because they are stringified).

What do you think is the better approach ?

— asked September 27th 2010 by David Waith
  • context
  • reference
  • titanium.app.properties
0 Comments

2 Answers

  • Let me add another variant to this discussion:

    3) Using the App object:

    app.js:

    Ti.App.myData = ["first", "second", "third"];
    

    winSub.js:

    var myItem = Ti.App.myData[0];
    

    A first short test indicated that this approach works. Basically, this approach uses Ti.App as a global data container. However, it would be really preferable to have a comfortable way of passing references to Window contexts rather than using global variables.

    Regarding your approaches 1) and 2), I like 1) better because 2) is restricted to the object types that Ti.App.Properties provides. Also, Ti.App.Properties represents persistent data that the system shall store between sessions, so it does not seem appropriate for in-session data storage.

    UPDATE:

    It appears that the object in Ti.App becomes immutable. That is, sth. like

    Ti.App.myData.push("fourth");
    

    does not work for me (Titanium SDK 1.4.1.1, iPhone SDK 4.0). Ti.App.myData still holds the initial contents. :-(

    — answered October 19th 2010 by Christoph Berger
    permalink
    0 Comments
  • This is a great question, David. Thanks for sharing the two approaches. Now, if we could just get an official word on this. Maybe someone from Appcelerator could chime in on this?

    — answered September 28th 2010 by Christopher Rumpf
    permalink
    0 Comments
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.