Friday, April 25, 2014

Destroy of Dynamically created stores

         Often a need arises when we create stores dynamically and not through the general MVC structus, Since those stores are global (app scope). We may want to create stores dynamically for select fields per screen,lists,etc.

         While working on one such scenario, I had a doubt on the destruction of these stores.
I installed the chrome plugin from Sencha: https://chrome.google.com/webstore/detail/app-inspector-for-sencha/pbeapidedgdpniokbedbfbaacglkceae?hl=en

What you can do is open a Screen using dynamic stores, Go to the stores tab and you will notice all the stores listed in the App.
Now, Navigate to some other Screen, and come back to the screen you visited, The store count keeps on increasing and the previous stores are never destroyed.

I went through the Grid source Code: http://docs.sencha.com/touch/2.3.1/source/Grid.html#Ext-grid-infinite-Grid. You will notice that Sencha has explicitly set the autoDestroy to true.

Hence, also checked the store source code: http://docs.sencha.com/touch/2.3.1/source/Store.html#Ext-data-Store and noticed that for Stores the autoDestroy config is set to false by default, as opposite to any components where its set to true by default.

For stores that are declared globally, under the stores namespace, they are not destroyed throughout the app. We are ensuring that we clean records in them. However, for dynamically created stores, we will need to set the autoDestroy to true or destroy them explicitly. If this is not done the stores along with the records keep on increasing as the screens are visited.

This will certainly give some improvement in terms of memory!. 
Feel free to discuss any views on this :).