self.view Objects Cleanup

I am trying to understand how to efficiently remove scene objects once a scene is destroyed. According to corona docs objects can be inserted to self.view in order to clean them up once a scene is destroyed.

Here is my code that I am trying

local composer = require( "composer") local scene = composer.newScene() local rects={} local rectsNum = 0 local rectsCount = 0 local rectsTimer local sceneGroup local function rectTransComplete( obj ) print("rectTransComplete .. called") rectsCount = rectsCount + 1 if rectsCount == 3 then composer.removeScene("scene2") end end local function spawnRect( ) print("inside spawnrects") rectsNum = rectsNum + 1 rects[rectsNum] = display.newRect( display.contentWidth/2, 100, 100, 100) rects[rectsNum]:setFillColor( 1,1,1 ) rects[rectsNum].id = numRect rects[rectsNum].transition = transition.to( rects[rectsNum], {time = 9000, y = display.contentHeight + 100, onComplete = rectTransComplete } ) sceneGroup:insert( rects[rectsNum] ) end function scene:create( event ) sceneGroup = self.view end function scene:show(event) print("inside create") if event.phase == "did" then rectsTimer = timer.performWithDelay( 1000, spawnRect, -1 ) end end function scene:hide( event ) -- body end function scene:destroy( event ) timer.cancel( rectsTimer ) print("scene Destroyed") end scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) return scene

It seems to work fine since all the rectangles are removed from the screen once the scene is destroyed but I see the app printing messages “rectTransComplete … called” even after the scene is destroyed. Why is it so and why are the transitions not automatically cancelled once the objects are removed. Do I need to cancel all transitions manually by iterating through all the rectangles and cancel each transition. What about the rects table as it also contains references to the rects. Should I manually clean it up. If I need to manually clean everything (or most of the things), what exactly is the benefit of using self.view

Hi @zahidbashirkhan,

Transitions and timers are not automatically cancelled when the display object(s) are removed. The auto-removal of these objects inserted into the scene view are a convenience for the display objects themselves, not other elements associated with them (timers, transitions, event listeners, other variables handles/references, etc.).

Note that you can “tag” transitions and then cancel all transitions of the same tag name. Or, you can cancel all transitions outright. So, it’s probably not necessary to store them all in a table and iterate through them to cancel… however, doing that is totally acceptable if you want.

Best regards,

Brent

Thanks

Could you please elaborate the “tag” option. Any link of how it is done?

Hi @zahidbashirkhan,

The documentation for transitions, including how to use the tag feature, are located here:

http://docs.coronalabs.com/api/library/transition/index.html

Take care,

Brent

Hi @zahidbashirkhan,

Transitions and timers are not automatically cancelled when the display object(s) are removed. The auto-removal of these objects inserted into the scene view are a convenience for the display objects themselves, not other elements associated with them (timers, transitions, event listeners, other variables handles/references, etc.).

Note that you can “tag” transitions and then cancel all transitions of the same tag name. Or, you can cancel all transitions outright. So, it’s probably not necessary to store them all in a table and iterate through them to cancel… however, doing that is totally acceptable if you want.

Best regards,

Brent

Thanks

Could you please elaborate the “tag” option. Any link of how it is done?

Hi @zahidbashirkhan,

The documentation for transitions, including how to use the tag feature, are located here:

http://docs.coronalabs.com/api/library/transition/index.html

Take care,

Brent