简体   繁体   English

在appcelerator Titanium中进行数据绑定的最佳做法是什么,避免内存崩溃

[英]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. 我一直在使用自定义事件来处理与钛appcelerator的数据绑定,对于给定的对象ID(所有对象唯一)和传递新值的属性,使用称为RefreshComp的事件。 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... 这个新值可能来自推送通知,您希望在iphone应用中编辑的对象要传播到所有伴奏等。

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 然后您的eventHandler函数可能像

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. 我的观点是,这会使comp绑定到全局上下文,从而导致对象无法释放。 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 因为绑定引用的组件仍然在IOS端的内存中,但是当TiViewProxy.m在_hasListeners中调用父函数时,它无法在内存中找不到没有任何关联对象的父对象,并且该父对象已经被从内存中删除

I have googled and look at the git rep of appcelerator but there are no example of this. 我已经用谷歌搜索并查看了appcelerator的git rep,但是没有这个例子。

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. 我通常倾向于使用一种干净简单的结构,其中每个js文件都由一个ui元素组成。 this provides a clean way of managing your ui. 这提供了一种管理用户界面的干净方法。 and if your component listens to an event like globalRefreshEvent it refreshs itself. 如果您的组件侦听诸如globalRefreshEvent之类的事件,它将刷新自身。

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. 希望它能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Titanium Appcelerator中的setTimeout内存泄漏 - setTimeout memory leak in Titanium Appcelerator 什么是打开钛制燃烧器窗口的正确方法? - What is the right way to open titanium appcelerator windows? 报纸/杂志的钛制加速器数据存储 - titanium appcelerator data storeage for newspaper/magazin 将复杂的JS插件添加到Rails的最佳实践是什么? - What are the best practises when adding complex JS plugins to Rails? Commonjs-在Appcelerator中使用Titanium将数据从一个窗口移动到另一个窗口 - Commonjs - Moving data from one window to another using Titanium in Appcelerator 从 Appcelerator Titan 中的远程 XML 文件获取数据 - Getting data from a remote XML file in Appcelerator titanium Appcelerator钛合金:如何在ScrollableView上存储数据以在单击事件中使用 - Appcelerator Titanium Alloy: How to store data on a ScrollableView to use in click event HTTPClient调用未在Appcelerator Titanium应用中返回正确的JSON数据 - HTTPClient call is not returning correct JSON data in Appcelerator Titanium app 触摸获取像素数据-Android / iPhone Appcelerator Titanium - Get pixel data at touch - Android/iPhone Appcelerator Titanium Titanium Appcelerator sqlite数据库 - Titanium appcelerator sqlite database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM