I have code I want it run in click button home or go to background device.
self.ref.child("khlea").child("Users").child(self.user_random!).setValue(nil)
I have code I want it run in click button home or go to background device.
self.ref.child("khlea").child("Users").child(self.user_random!).setValue(nil)
You can do this in two ways far as i know.
Write your code in applicationWillResignActive() method in appdelegate and sceneWillResignActive method in scenedelgate.
NotificationCenter to observe the UIApplicationWillResignActive event. This event is fired every time the application will enter the background.
func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
NotificationCenter.default.addObserver(self, selector: #selector(saveToFirebase), name: UIApplication.willResignActiveNotification, object: nil)
}
func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
NotificationCenter.default.removeObserver(self)
}
@objc func saveToFirebase(){
self.ref.child("khlea").child("Users").child(self.user_random!).setValue(nil)
}
In Swift 4.1 + Closure Version
var resignObserver: NSObjectProtocol!
override func viewDidLoad() {
super.viewDidLoad()
resignObserver = NotificationCenter.default.addObserver(forName: UIApplication.willResignActiveNotification, object: nil, queue: nil) { _ in
self.ref.child("khlea").child("Users").child(self.user_random!).setValue(nil)
}
}
deinit {
NotificationCenter.default.removeObserver(resignObserver!)
}