17

I'm trying to get into SwiftUI right now, but struggeling with basic things. My struggle of the day: I'm wondering if there is something like a controller in SwiftUI? Where does logic not related to the UI go?

To give a concrete example:
I have an external framework. When the app starts, I fire up that framework (right now in the AppDelegate). It does some networky stuff and based on the result, I show one of two views. In one of them, the user has to input something, presses the OK button and I have to pass the input back to the Framework. How would I go about doing that?
I have no controller or coordinator and putting it in the View (through Singletons) seems wrong to me.


Sidenote: Interestingly, every single tutorial I find just omits stuff like this. They all focus on one more views, and a ViewModel, but never "zoom out" to show how an entire application (beyond two views linked with NavigationLink) work. If somebody can point me to one that tackles this, I would be very greatful.

BlackWolf
  • 5,239
  • 5
  • 33
  • 60
  • 1
    Although there is a `HostingViewController` behind the scene, SwiftUI is based on MVVM pattern. So no, there is no View**Controller** you should search for **ViewModel** instead. – Mojtaba Hosseini Oct 29 '19 at 09:39
  • I see. So we would inject a ViewModel from the HostingViewController into the View? And the ViewModel of the root view would then take care of initializing the framework, communicating with it and passing that along to the view through bindings? – BlackWolf Oct 29 '19 at 09:42
  • 1
    check this tutorial : https://medium.com/better-programming/making-a-real-world-application-with-swiftui-cb40884c1056 – user1105951 Oct 29 '19 at 09:44
  • @MojtabaHosseini So to better understand and write apps using SwiftUI it is worthwhile learning more about MVVM? – Joakim Danielson Oct 29 '19 at 09:51
  • 2
    Yes @JoakimDanielson . But there is no *best practice* out there for SwiftUI *yet*. Also keep in mind, It's proven that Apple patterns and logics they use are different with the outside world. For example Apple MVC is little different with other MVCs, or apple websocket works differently with others. So maybe you find some differences in Apple MVVM with other MVVM workflows. But the short answer is **yes**. It worth. – Mojtaba Hosseini Oct 29 '19 at 09:56
  • @BlackWolf it depends. maybe `@Environment` is what you need. – Mojtaba Hosseini Oct 29 '19 at 09:59
  • @BlackWolf so agree on your sidenote, no tutorial out there, including apple's official tutorial shows how to add backend logic/actions – Abhinandan Dubey Oct 31 '20 at 18:07
  • Unfortunate that there is no good answer to this, I am starting with SwiftUI and am struggling with the very same thing. Did you find something that helped you grasp the bigger picture? @BlackWolf – Big_Chair Jul 04 '21 at 15:28

1 Answers1

7

Sidenote: Interestingly, every single tutorial I find just omits stuff like this.

That is because MVVM doesn't put emphasis on control. It is built around having model-view binding. MVVM does not prevent you from writing bad control codes, and it requires binding mechanism not present in iOS until SwiftUI. It's by no means solution to everything as some Internet articles would like you to believe.

SwiftUI is influenced by MVVM? yes.

SwiftUI is based on MVVM? no. I'd argue it is more React-like.

Having no view controller does not mean you should do control in view model.

It just means you don't need an extra object to do control. Function itself is control.

In SwiftUI you use @State for local state, and function to mutate it. This is where you will find control.

There are also other mechanisms to import external state such as @EnvironmentObject.

And state changes would trigger view update, as React.

Some MVVM developer and their mother would create an object called view model with no binding support whatsoever; then spend a bunch of hours doing manual binding with Combine, and after several refactor commits later tell you MVVM is the second coming of Christ and best hope for clean architecture.

You don't have to. Obvious I'm opinionated, so take it with a grain of salt.

You only have to look for where and how SwiftUI does binding, and you will know view model is redundant. So no, SwiftUI is not based on MVVM. It can have model-view mapping without ever creating any view model. You don't need to learn MVVM to understand any of that.

Managing control in SwiftUI is easier if your control are functions. The removal of view controller means you don't have to worry about its life cycle and internal states. You can manage them through protocols and even introduce some functional programming paradigms.

If you take a closer look at SwiftUI design, you would notice the heavy usage of value type and removal of objects and their life cycle. Both are in direct opposition of MVVM paradigms.

Every MVVM developer uses reference type as view model; SwiftUI wants you to map value type to view. i.e.; struct Model: View. Swift is not Java, but you won't see anyone mention that.

Jim lai
  • 1,224
  • 8
  • 12
  • 2
    So more practically this means extracting as much of the control logic into separate/smaller controllers, but otherwise, handle all wiring logic inside SwiftUI Views? – Zorayr May 05 '20 at 21:05