- StackOverflow link: https://stackoverflow.com/questions/45955583/detect-when-a-view-controller-goes-to-background-and-gets-resumed
override func viewWillAppear(_ animated: Bool) {
NotificationCenter.default.addObserver(self, selector: #selector(didBecomeActive(notification:)),
name: UIApplication.didBecomeActiveNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(willResign(notification:)),
name: UIApplication.willResignActiveNotification, object: nil)
}
override func viewWillDisappear(_ animated: Bool) {
NotificationCenter.default.removeObserver(self, name: UIApplication.didBecomeActiveNotification, object: nil)
NotificationCenter.default.removeObserver(self, name: UIApplication.willResignActiveNotification, object: nil)
}
- StackOverflow link: https://stackoverflow.com/a/32688124/4620598
- loadView
- loadViewIfNeeded
- viewDidLoad
- viewWillAppear
- viewWillLayoutSubviews
- viewDidLayoutSubviews
- viewDidAppear
- viewWillDisappear
- viewDidDisappear
- Stack overflow: https://stackoverflow.com/q/1210047/4620598
The bounds of an UIView is the rectangle, expressed as a location (x,y) and size (width,height) relative to its own coordinate system (0,0).
The frame of an UIView is the rectangle, expressed as a location (x,y) and size (width,height) relative to the superview it is contained within.
We can define a memory leak as a portion of memory that is occupied forever and never used again. It is garbage that takes space and causes problems.
In iOS the number one reason of memory leaks is retain cycles
. A retain cycle is created when two objects have a strong reference to the other object.
The solution is to use unowned
or weak
modifier in one of the references.
https://medium.com/flawless-app-stories/memory-leaks-in-swift-bfd5f95f3a74
Difference between unowned and weak
Both of them are weak references. The main diference is that weak
is optional while unowned
isn't. In this way, an unowned
reference is always suppose to have a value. Otherwise, the app will crash.
Use unowned
when you know that the life of object that holds this reference will be always shorter than the referenced object. Example: Credit card holding a reference to an Account. Credit card life will be always shorter than the account.
Otherwise, use weak
https://medium.com/ios-os-x-development/ios-architecture-patterns-ecba4c38de52
- MVC
- MVP
- MVVM
- VIPER