Using [weak self] by default
A minor follow up point to the post using Combine to manage input events is to use [weak self]
in closures that refer to self
.
I'm sure this is a very obvious point to Apple platform developers, as - once you know to look for it - the whole internet is full of comments on avoiding retain cycles.
I noticed input acting weirdly, and triggering breakpoints that should have been unaccessible anymore. The reason was that I managed to capture a reference in some closures to the scene (which was stored on the scene itself, so neither end could release the reference). So when a scene transition happens, the old scene does not get discarded, and all the input handling machinery keeps working in the background.
While it is easy to fix, I wonder if there is a good way to systematically be sure that these cycles do not get created, and that deinit
gets called for every scene. E.g. maybe I can track inits/deinits, and see if the numbers arebalanced in a debug console, or something like that.
Food for thought.