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