I completed a spritekit game without having knowledge of strong reference cycles. I'm not having any crashes or obvious problems, but using instruments I can see my objects being retained between each level (SKScene should be dealloced between levels).
Here are the general pieces of my game:
GameConfig
static struct
I have a globally accessible static struct that references my ViewController. I access the viewController using this struct from all over the app. I assign GameConfig.viewController = self
inside of the viewController's viewDidLoad
GameViewController
In here I reference my SKScene, and SKView as such
var skView: MyView!
weak var scene: SKScene!
BaseScene
This is my base SKScene class. This is the only property of this class that gets a reference to the class itself
let indicators: Indicators = Indicators()
KillScene
subclass of BaseScene
In my BaseScene subclass, I have many custom subclasses contained in arrays. These custom subclasses do reference the scene, but the scene is not referencing them directly. It only has reference to their container array. Example:
var radarBlips: [RadarBlip] = []
I do have one property that has a strong reference cycle with KillScene
var ship: KillShip = KillShip()
In both my Indicators
and KillShip
classes, I have referenced my scene using the weak
keyword.
In fact I have found every instance where I reference the scene, and put weak
in front of it.
I tried to make a stripped down game, and create a strong reference cycle between a sprite and a scene, and then break it. I was successful in doing so. But I can't seem to debug my own game.
Any detailed suggestions would be helpful. I know there's trickiness with passing closures around. Maybe that has something to do with it? Any advice on what I could look for would be great. I've put weak
in more places than I probably need to.
I can not get the scene to deinit
There wasn't an easy way to do this. Check all your properties that reference eachother and use weak or unowned. The bigger issue in spritekit is checking for closures. When you're running delayed closures (in SKActions) and things like that it's important to use [weak self]
within the closure so it doesnt keep the object alive. It's easier to add more weaks and unowned than necessary until the strong reference cycle is broken and then work your way backwards deleting unnecessary ones.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments