MVC (Missing View Controller)
Where is UIViewController in SwiftUI?
This year of WWDC is really big for the Apple developer community. Apple’s new declarative UI framework, SwiftUI, is undeniable steal the show. It caught everyone attention including me.
After the keynote I download Xcode beta to try out this new shiny view. At first glance, I thought SwiftUI is a V (View) in MVC, but when I start a new project with SwiftUI I was surprised, there is no UIViewController or anything similar, all I got is view.
I feel lost, thousands of questions pop up in my mind if there is no UIViewController who gonna listen to delegate, target/action, notification, and do all the hard works.
Turn out I ask the wrong question, what I should ask is what is the real jobs of a controller. All that delegate, target/action, and notification are just different ways to communicate some knowledge around. If I say there is a new paradigm that handles this communication in a different way, do we still need a controller?
Deeper Down the Rabbit Hole
As you dig deep into this new declarative ui you will find out this is not just a V (view) in MVC anymore it packed with many features that use to need a lot of controller and communication to be able to work i.e. interruptible, navigation, and many good things from UIKit. Another reason Apple can get rid of C (controller) is that they also introduce a new framework for data communication, Combine.
The Combine framework provides a declarative Swift API for processing values over time. These values can represent user interface events, network responses, scheduled events, and many other kinds of asynchronous data, this makes getting rid of controller possible.
MVC is dead, long live MVC and Reactive
Apple introduce us to reactive architecture, but this doesn’t mean you have to drop MVC completely (I know most reactive library out there is kind of all or nothing approach, you either use it or not at all), Apple design in a way we can easily bridge between UIKit and SwiftUI world, so you can get the benefit of both world, you can gradually migrate your UIKit to SwiftUI piece by piece where you see fit and if you start your project in SwiftUI and hit a roadblock knowing you can switch back to UIKit anytime give you peace of mind.
Community mock MVC (Massive View Controller) for many years and plenty of architectures arise more than I can count, I’m happy that finally we have a new native architecture which encourage us to write less code not more (like some architecture out there). Personally, I don’t think this will be the end of UIKit and MVC. I think it will coexist for quite some time, community have to try this out hit some roadblocks to prove this new architecture.
SwiftUI is still young it would take many years before it reaches prime time. But even at this early stage, it shows a promising future to me and I can’t wait to use it for my next project.