2013-08-22 44 views
1

我在Appcelerator中使用Alloy框架,并在Apple测试我的应用程序时一直在努力处理内存泄漏问题。Appcelerator内存泄漏 - 合金框架

我有一个可滚动的视图,视图是可滚动视图和视图的视图或“页面”,如“页面”视图的孩子的缩略图。所有这些视图都是动态创建的,然后在用户执行重新加载可滚动视图内容的搜索时将其删除并重新创建。

我的问题是,即使我去掉滚动视图,并将其设置为NULL仪器现场字节继续进行搜索每次和创建新的滚动视图成长。我应该如何处理这些UI元素,以便垃圾回收来移除它们?

var videoSlider; 

function loadData(searchTerms,channel,sortBy,limit) { 
if (videoSlider) { 
    $.scrollableViewHolder.remove(videoSlider); 
    videoSlider = null; 
} 

videoSlider = Alloy.createController('videoSlider', {}).getView(); 
$.scrollableViewHolder.add(videoSlider); 

var viewSliderArray = []; 

feeds.GetFeeds({ 
    success: function(data) { 
     Ti.API.info("Number of videos returned from Brightcove " + videosObject.items.length); 
     var j = 0; 
     for(var i=0; i<videosObject.items.length; i++) { 
      if(i % 8 == 0) { 
       Ti.API.info(i); 
       if(i > 0) { 
        viewSliderArray.push(viewSlider); 
       } 
       viewSlider = Alloy.createController('viewSlider', {}).getView(); 
       j = 0; 
      } 

      tempTime = videosObject.items[i].length/1000; 
      minutes = Math.round(tempTime/60); 
      seconds = Math.round(tempTime%60); 
      seconds = "0"+seconds; 
      seconds = seconds.substr(seconds.length-2); 

      videoLength = minutes+":"+seconds; 

      videoBox = Alloy.createController('videoBox', { 
       videoBoxTop: videoBoxTop[j], 
       videoBoxLeft: videoBoxLeft[j], 
       videoStill : videosObject.items[i].videoStillURL, 
       videoTitle: videosObject.items[i].name, 
       videoLength: videoLength 
      }).getView(); 

      viewSlider.add(videoBox); 
      j++; 
     } 
     viewSliderArray.push(viewSlider); 

     Ti.API.info(viewSliderArray); 

     videoSlider.views = viewSliderArray; 
    } 
},searchTerms,channel,sortBy,limit); 

} 
+0

你解决了这个问题吗? –

回答

0

从scrollview中删除所有的儿童,并将它们设置为null。以下是可能对您有用的示例代码:

exports.removeChildren = function(thisView){ 
if (thisView.children) 
{ 
    var removeData = []; 
   for (var i = thisView.children.length; i > 0; i--) 
     { 
       removeData.push(thisView.children[i-1]);   
   }; 
   // Remove childrens 
   for (var i = 0; i < removeData.length; i++) 
   { 
         thisView.remove(removeData[i]); 
   } 
   removeData = null; 
} 
} 
0

请尝试此操作以清除您的视图的记忆。

clean(Your_View); 
Your_View = null; 

function do_clean(e, c) { 
    try{ 
     clean(c); 
    e.remove(c); 
    c = null; 
    }catch(ex){ 
     Ti.API.info('Exception in removing child: '+ex +'children :'+c); 
    } 
    return; 
} 

function clean(e) { 
    if (e != null) { 
     if (e.children) { 
      for (var i = 0; i < e.children.length; i++) { 
       if(e.children[0] != undefined && e.children[0] != null) 
        do_clean(e, e.children[0]); 
      } 
     } else { 
      return; 
     } 
    } 
} 

也尝试删除所有不需要的eventListeners。

0

您需要在scrollview上调用removeAllChildren,并将viewSliderArray设置为一个空数组,即viewSliderArray = [],否则引用将被保留。

而且在使用仪器可以搜索TiUI,看看哪个观点是明确的增加,这样你可以针点这是给你的麻烦了特别的看法。