UIKit <-segue->Memory Leak in Transition Between SpriteKit

Asked 2 years ago, Updated 2 years ago, 39 views

Hello,

Swift is developing an iPhone app.

Use Segue to use ViewController for UIKit and
I made it possible to go back and forth between GameViewController for SpriteKit.
When running on an iPhone simulator, the debug navigator has
memory. Every time I switch the screen, it increases by 30M and I'm having a headache.
I was able to do what I wanted to do, but I'm frustrated.
If you don't mind, please let me know.

Environment
OSX 10.9.5 Xcode.6.1.1

Below is an excerpt of each file.

class FirstViewController:UIViewController {
    @ IBAction func returnMenu (segue:UIStoryboardSegue) {
        println("Welcome back!")
    }

    override functouchesEnded(touches:NSSet, withEvent:UIEvent) {
        performSegueWithIdentifier("toGame", sender:nil)
    }
}


classGameViewController:UIViewController {

    override func viewDidLoad(){
        super.viewDidLoad()

        iflet screen=GameScene.unarchiveFromFile("GameScene") as? GameScene{
            scene.viewController=self
            skView.presentScene(scene)
        }
    }
}

classGameScene:SKScene{

    var viewController:UIViewController?

    override functouchesBegan(touches:NSSet, withEvent:UIEvent) {
        for touch:AnyObject into touches {
        self.viewController?.performSegueWithIdentifier("back", sender:nil)
    }
}

I look forward to your kind cooperation.

swift

2022-09-30 20:45

1 Answers

Memory "leaks" (a phenomenon in which unused memory is not released) can avoid reference cycles by making the class GameScene property viewController weakly referenced.(As pointed out in the comment section)

Another solution here is to leverage the Responder Chain for touch events.

class FirstViewController:UIViewController {
    @ IBAction func returnMenu (segue:UIStoryboardSegue) {
        println("Welcome back!")
    }

    override functouchesEnded(touches:NSSet, withEvent:UIEvent) {
        performSegueWithIdentifier("toGame", sender:nil)
    }
}


classGameViewController:UIViewController {

    override func viewDidLoad(){
        super.viewDidLoad()

        iflet screen=GameScene.unarchiveFromFile("GameScene") as? GameScene{
            // scene.viewController=self//Delete
            skView.presentScene(scene)
        }
    }

    // Ported from the GameScene class.
    override functouchesBegan(touches:NSSet, withEvent:UIEvent) {
        for touch:AnyObject into touches {
        self.performSegueWithIdentifier("back", sender:nil)
    }
}

classGameScene:SKScene{

    // var viewController:UIViewController?//deleting
/* Delete touch events.Port to GameViewController.
    override functouchesBegan(touches:NSSet, withEvent:UIEvent) {
        for touch:AnyObject into touches {
        self.viewController?.performSegueWithIdentifier("back", sender:nil)
    }
*/
}

A responder chain of touch events is a mechanism in which touch events are passed between subclasses of the UIResponder in order until a receiver (method) is found to receive the touch event.If SKNode (SKScene) does not receive a touch event (touchBegan), it is passed to a subclass (SKView) in the UIView and to a subclass (GameView Controller) in the UIView Controller, or to a UIWindow.If you go all the way to UIA application and it is not dealt with there, you will be sent to the darkness of the abyss.
The responder chain allows the GameScene class to pass touch events to GameViewController in a way that does not dare to process them.


2022-09-30 20:45

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.