Search code examples
swiftquickblox

Swift: functions run several times per load viewcontroller


I use below code in viewController.swift

func didReceiveNewSession(_ session: QBRTCSession, userInfo: [String : String]? = nil) {
    print("PLAY SESSION")
}

I open application and after receive session i get one log PLAY SESSION

But if i open application and i go to viewControllerPage.swift and back again to viewController.swift and after that receive session i get two log PLAY SESSION

As the result, if i open viewController.swift 7-8 times, after receive session print 7-8 PLAY SESSION

I want only get once session even i open every time viewController.swift

I want didReceiveNewSession load once


Solution

  • this is exactly what viewDidload and viewDidAppear are for

    override func viewDidLoad() {
        //insert the code you only want to run once here
    }
    
    override func viewDidAppear(_ animated: Bool){
        //insert the code you want to run every time you load the VC
    }
    

    was there a particular reason why you did not want to use them? alternatively you could use viewWillAppear() instead of viewDidAppear()