MVVM

The interviewer asks

“Explain MVVM. How does it improve on MVC?”

How to answer

MVVM puts a view model between the view and the model: it owns the screen's state, runs presentation logic and calls services, while the view only renders that state and forwards user actions. Compared with MVC, logic moves out of the massive view controller into a plain class you can unit test without UI. In SwiftUI today the view model is a @MainActor @Observable final class whose dependencies are injected through init behind a protocol, and the view owns it with @State. Binding is automatic: the view re-renders when a property it read in body changes (see the @Observable macro answer for how tracking works).

Key points

  • View model: screen state (loading, data, error) and intents (load(), retry())
  • View: renders state, sends actions; no business logic
  • Binding: Observation tracks the properties body reads, no @Published or Combine needed
  • Dependencies come in through init as protocols, so tests can pass fakes
  • @MainActor because the state drives UI

If they dig deeper

  • Is MVVM even needed in SwiftUI? A SwiftUI View is already a lightweight description of state, so simple screens can keep @State in the view and call a service directly. A view model earns its place when there is real presentation logic, async loading, or something you want to unit test. Name the trade-off rather than picking a side dogmatically.
  • UIKit: bind manually, or rely on automatic observation tracking (iOS 26): reading @Observable properties in viewWillLayoutSubviews or updateProperties() re-runs that method when they change.
  • Navigation usually lives in a coordinator or router, not the view model.

Common pitfalls

  • Creating the dependency inside the view model (let service = LiveUserService()) so it can't be tested
  • Passing UI types (UIColor, Image, UIViewController) into the view model
  • Leftover @StateObject / @Published with an @Observable class; with Observation the view owns it via @State
  • A "god" view model that serves several screens

How to use it

swift
protocol UserService: Sendable {
    func fetchUser() async throws -> User
}

@MainActor
@Observable
final class UserViewModel {
    private(set) var user: User?
    private(set) var isLoading = false
    private(set) var errorMessage: String?

    private let service: any UserService
    init(service: any UserService) { self.service = service }

    func load() async {
        isLoading = true
        defer { isLoading = false }
        do {
            user = try await service.fetchUser()
            errorMessage = nil
        } catch {
            errorMessage = String(localized: "Couldn't load your profile.")   // details go to the log
        }
    }
}

struct UserView: View {
    @State private var viewModel: UserViewModel

    init(service: any UserService) {
        // SwiftUI keeps only the first instance; keep the view model's init cheap
        _viewModel = State(initialValue: UserViewModel(service: service))
    }

    var body: some View {
        Group {
            if viewModel.isLoading { ProgressView() }
            else if let message = viewModel.errorMessage { Text(message) }
            else { Text(viewModel.user?.name ?? "") }
        }
        .task { await viewModel.load() }
    }
}

Before iOS 17's Observation, view models were class UserViewModel: ObservableObject with @Published properties and @StateObject in the view. That code is legacy, not deprecated: it still builds cleanly in Xcode 27, so keep it in existing code or Combine pipelines and use @Observable for new view models.

Best practices

  • Keep the view model UI-agnostic; expose plain values the view formats
  • Inject services through init behind a protocol (any UserService or a generic parameter)
  • Mark it @MainActor @Observable final class; make state private(set) and change it through methods
  • Own it with @State in the view that creates it; pass it down as a plain reference
  • Test it in isolation with a fake service

Related concepts

  • MVC, view model design, state management, @Observable macro, view model testing, dependency injection, coordinator pattern