jbpringuey
jbpringuey

Reputation: 21

What are the best practises for data binding in appcelerator titanium and avoid memory crash

I have been using custom events to handle data binding with titanium appcelerator using an event called RefreshComp for a given object id (unique across all objects) and attribute passing the new value. This new value could come from a push notification , an object beeing edited in the iphone app that you want to propagate to all the comps etc...

Titanium.App.fireEvent(RefreshComp, {
    refreshid : objectId + '-' + attribute,
    value : newvalue
 });

and

function registerEvent(objectId,attribute,eventHandler){
    Titanium.App.addEventListener(RefreshComp, function(e) {
        if((e.refreshid === (objectId + '-' + attribute))) {
            eventHandler(comp, e.value);
        }
    });
}

and then your eventHandler function could be as simple as

function eventHandler(comp,newvalue){
    comp.value = newvalue;
}

or more complicated (like changing background etc...)

My point is that this causes the comp to be bound to the global context causing the object not to be release. I have tried to attach custom event to the component itself but it does not work. As a result I am getting the application that is crashing with

Exception Type:  EXC_BAD_ACCESS (SIGSEGV)
Exception Codes: KERN_INVALID_ADDRESS at 0x242424cf

because the binding refer to a component still in memory on the IOS side but then when TiViewProxy.m calls for the parent function in _hasListeners , it is not able to find the parent in memory that does not have any associated object and the parent has been removed form the memory

I have googled and look at the git rep of appcelerator but there are no example of this.

Upvotes: 0

Views: 1122

Answers (1)

mkind
mkind

Reputation: 2043

there a millions of way to refresh special components. i usually tend to have a clean and simple structure where every js file constists of one single ui element. this provides a clean way of managing your ui. and if your component listens to an event like globalRefreshEvent it refreshs itself.

createMyComponent = function(_args){

   var myComp = Ti.UI.createWhatEver({
      property0 = _args.prop0 || "defaultValueForProp0"
      property1 = _args.prop1 || "defaultValueForProp1"
      value = _args.value || "defaultValueForValue"
  });

  myComp.refreshValue = function(_newValue){
       myComp.value = _newValue;
  };

  Ti.App.addEventListener('globalRefreshEvent',function(e){
       myComp.refreshValue(e.value)
  });

  return myComp;
};

create your comp like that:

var comp = createMyComponent();
view.add(comp);

hope it helps a little bit.

Upvotes: 0

Related Questions