Hello people, Leo here. Today I want to explain what was the old view lifecycle and why it’s already obsolete. First of all, what I’m calling the “old view life cycle” is technically the UIView lifecycle. It has some method that we can override to enable our screen to behave the way and when we wanted.
The Old UIView View Lifecycle
The methods are listed below :
- loadView
- viewDidLoad
- viewWillAppear
- viewWillLayoutSubviews
- viewDidLayoutSubviews
- viewDidAppear
- viewWillDisappear
- viewDidDisappear
- viewDidUnload
It’s not the scope of this article to explain each one of those but it’s important to know how and why to override them in your code. For example: Early on in my development learning I thought that viewDidLoad was called every time a view is called… I couldn’t be more wrong.
The viewDidLoad is called just ONE time for each view when… guess what… the view was loaded. The method I should override was viewWillAppear to achieve the behavior that I wanted. So this kind of basic understanding is essential to any developer until now because now we have the almighty SwiftUI and all these lifecycle methods were gone for good. ( at least in pure SwiftUI code base)
In SwiftUI you will not have a lifecycle, you will have only the events occurring throughout your app. You have a large Tool box of states and data flows that you can use all around your app. And yes, if you are thinking the environment is just a big singleton, you are right and we no one died because of that, just get over it.
And you also have the :
onAppear – onDisappear
For onAppear and onDisappear, they are available as properties on View and mock the appear-like events in the old lifecycle, this is very useful when we are thinking about migrating behaviors from old apps to SwiftUI apps.
That’s the resume of it, the future is bright and we no more need to care about all the view lifecycle because SwiftUI deal with states and data flows.
Summary
That’s all my people, I hope you liked reading this article as much as I enjoyed writing it. If you want to support this blog you can Buy Me a Coffee or just leave a comment saying hello. You can also sponsor posts and I’m open to freelance writing!
You can reach me on LinkedIn or Twitter and send me an e-mail through the contact page.
Thanks for the reading and… That’s all folks.
Credits: image