MVVM and SwiftUI

I’m still learning SwiftUI but I’m wondering if navigation and dependency injection are still done more or less the same way as is shown in the book or if it’s completely different?

1 Like

hi @mfilipowicz, in SwiftUI navigation is done similarly to the book. It’s not the exact same way, but similar in that both the book and SwiftUI navigation is driven off of value type models.

Dependency injection on the other hand is very different. For dependency injection, the official approach from Apple is to use @EnvironmentObject for reference type dependencies and @Environment for value type dependencies. Just a word of caution, be careful when using @EnvironmentObject because SwiftUI will crash/trap if for some reason an object of the dependency’s type is not provided by a parent’s view .environmentObject modifier.

I’ve explored applying the dependency container approach from the book towards SwiftUI, and so far I’ve found it to be tricky because you can’t necessarily trust SwiftUI view structs to hold onto the dependency containers, since they are value types and view hierarchies in SwiftUI get recomputed often; so views get recreated all the time. I suggest starting with @EnvironmentObject to see how well it fits your needs and exploring the dependency container approach if you’d like something different than @EnvironmentObject.

If you’re looking for examples of MVVM with SwiftUI, we recently released a really great tutorial that’s worth checking out: https://www.raywenderlich.com/4161005-mvvm-with-combine-tutorial-for-ios

Please let me know if you have any follow on questions or if I can clarify anything. Hope this helps. Cheers!

thanks! Will the book be updated for SwiftUI?

@mfilipowicz we just released the second edition which has a new short chapter on advice on getting your app’s architecture ready for SwiftUI. We are definitely considering updating the book with SwiftUI content. Because SwiftUI is so new we would like to see what architecture patterns come out of teams using SwiftUI in real world projects. In the meantime, feel free to ask any questions here if you have any architectural questions while working with SwiftUI.

2 Likes

Please clarify when book will be updated with swift ui patterns?

Hello @rcach, thank you for this information, for me the book Advanced iOS App Architecture is like a bible on iOS or macOS architecture,

And do you have a plan to rewrite the example or project on this book with SwiftUI ?

Actually i try to do the same architecture like MVVM on a side learning project and for me it’s not very clear between the dependency injection and how we need to create a View and ViewModel,

And how we use the navigation on SwiftUI like on the book with RxSwift and container view ?

@rcach Can you please help with this when you get a chance? Thank you - much appreciated! :]

Hello, there is no answer about book update since 6 april. Does it mean that you are not planning to write update? As you have mentioned in book we should wait for some best practices. It seems that swiftui 2.0 will be announced in june 22 but we didn’t get even update for swiftui 1.0. Just one small chapter.

@rcach Can you please help with this when you get a chance? Thank you - much appreciated! :]