Mohammad Azam Profile picture
Jul 17 12 tweets 3 min read
After working on SwiftUI using MVVM for few years, I am realizing that MVVM may not be the best fit for the job. I may end up writing a post about it but here are few thoughts about my experience.

* If you had success with MVVM and SwiftUI then more power to you!

#SwiftUI
1/N I have written many articles regarding SwiftUI and MVVM, I have written books and even created courses on SwiftUI and MVVM. But each time I implemented a solution in SwiftUI and MVVM I felt not comfortable. The main reason was that I was always fighting the framework.
2/N When you work on a large application using MVVM with SwiftUI and you want to share state then it becomes extremely hard.

The main reason is that you cannot access EnvironmentObject inside the view model. Well, you can pass it using a constructor but then it a pain.
3/N And most of the time, you end up getting confused that who is managing the state. One thing to note is that React and Flutter which are very similar to SwiftUI does not use MVVM pattern.
4/N All these frameworks works using the uni-directional flow. So, it would be worth it to use EnvironmentObject and just allow EnvironmentObject to be available to all the views.
5/N One complain about EnvironmentObject is that it will refresh a lot of views when any value changes. In those cases you can slice up the environment object (azamsharp.com/2022/07/01/sli…).
6/N Although you can use Redux with SwiftUI but Redux have a lot of moving parts. You can simply create a fetchService, which loads the data and make sure to put that fetchService in environmentObject. This allows it to create one single source of truth.
7/N Another comment about Model objects. Most of the time, your app in consuming an API. The data you get from the API is not the domain model, that is the DTO (Data Transfer Object). Your domain object lies on the server, most of the time.
8/N When I was creating the Core Data course. I kept fighting the framework and did not use @FetchRequest etc. I wrote a LOT of code to make sure that VM loads new data, when new records are added.
9/N Recently I was using the Realm framework and they exposes a lot of their property wrappers to the view. If I was using MVVM to implement the solution, I would have not taken the advantage of those wrappers.
10/N When SwiftUI came out, I also jumped on the MVVM bandwagon. Hey! I might even be the driver. But now I have driven very far and I realized that I was on the wrong path. At least this is my honest opinion. I don't want to fight the framework anymore.
11/N Solution: Well for me is to keep a single source of truth. Kind of like Redux pattern but without all the complexity of Redux. Check out how Apple is using SwiftUI and you will find out, they keep it ... SIMPLE.

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with Mohammad Azam

Mohammad Azam Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Don't want to be a Premium member but still want to support us?

Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us on Twitter!

:(