2013-04-07 64 views
0

我正在使用Titanium Appcelerator來使用JavaScript開發應用程序。他們建議使用CommonJS方法。關於CommonJS的簡要例子可以參見here管理CommonJS模塊的問題

對於我的生活,我仍然無法弄清楚如何構建我的代碼。

例子:

/* Homescreen.js */ 
exports.createHomescreen = function() { 

    //load all required modules first 
    var videoPlayer = require('ui/videoPlayerModule'); 

    var self = Ti.UI.createWindow({ 
     width:'100%', 
     height:'100%' 
    }) 

    var newPlayer = videoPlayer.createPlayer({ 
     width:100 
     height:50 
    }); 

    self.add(newPlayer); 
    return self; 
} 

的videoPlayerModule

/* videoPlayerModule.js */ 
exports.createPlayer = function (object) { 

    //load all required modules first 
    var self = Ti.UI.createWindow({ 
     width:object.width, 
     height:object.height 
    }); 

    var exitVideoButton = Ti.UI.createButton({ 
     width:100, 
     height:50 
    }); 

    exitVideoButton.addEventListener('click',function(e){ 
     self.close(); //When this window is closed, the memory isn't freed. 
     self = null;  //Still the memory isn't cleared 
    }); 

    self.add(exitVideoButton); 

    return(self); 
} 

我有,因爲每當我加載一個錄像機並關閉它,內存不會被清零內存分配問題。如果我再次打開videoPlayer,則會再次分配內存。因此,每次啓動videoPlayer時,我的應用程序的內存使用量都會增加。

我知道我的思維方式不對。我在這裏忽略了很簡單的事情。任何人都可以讓我知道我不正確的做法嗎?

+0

您使用哪一個Titanium SDK以及哪個平臺? – 2013-04-07 09:46:06

+0

我正在使用最新的Titanium SDK(3.0) 而我正在爲iOS構建 – wiseindy 2013-04-07 10:55:07

+0

那麼是否有與此相關的問題。通常應該用3.0.2.GA來修正。我猜你已經看了一下Titanium的[內存管理](http://docs.appcelerator.com/titanium/latest/#!/guide/Managing_Memory_and_Finding_Leaks)。 – 2013-04-07 12:42:06

回答

1

這是因爲您正在將Ti.UI.Window(從videoPlayerModule.js創建)添加到另一個Ti.UI.Window(在Homescreen.js中),您不應該這樣做。 Ti.UI.Window是一個基本的容器對象,你永遠不會將它添加到任何東西(通常),所以當你關閉窗口時,它仍然作爲容器窗口的子項之一被引用,所以它永遠不會消失。您的self = null;此時不做任何事情。

您需要選擇替換的視頻播放器的窗口,我會嘗試這樣的事情,而不是:

/* videoPlayerModule.js */ 
exports.createPlayer = function (object) { 

    var self = Ti.UI.createView({ 
     width:object.width, 
     height:object.height 
    }); 

    var exitVideoButton = Ti.UI.createButton({ 
     width:100, 
     height:50 
    }); 

    exitVideoButton.addEventListener('click',function(e){ 
     self.hide(); 
    }); 

    self.add(exitVideoButton); 

    return(self); 
} 

這不是一個完整的解決方案,你仍然會在內存中的觀點,但它是一個非常小的空間,然後一個完整的窗口,更好的方法來做到這一點將創建一次,然後show()hide()它需要時在主屏幕上下文中,另一種方法是通過父母,然後退出時從其父節點中刪除該視圖,但這可以解決您的內存問題。

+0

是的!明白了@Josiah 我會嘗試你提到的兩個選項。多謝老兄:)乾杯。 – wiseindy 2013-04-08 08:03:04