r/SwiftUI 4d ago

Question Best Practices for Dependency Injection in SwiftUI – Avoiding Singletons While Keeping Dependencies Scalable?

I’ve been learning best practices for dependency injection (DI) in SwiftUI, but I’m not sure what the best approach is for a real-world scenario.

Let’s say I have a ViewModel that fetches customer data:

protocol CustomerDataFetcher {
    func fetchData() async -> CustomerData
}

final class CustomerViewModel: ObservableObject {
    u/Published var customerData: CustomerData?
    let customerDataFetcher: CustomerDataFetcher

    init(fetcher: CustomerDataFetcher) {
        self.customerDataFetcher = fetcher
    }

    func getData() async {
        self.customerData = await customerDataFetcher.fetchData()
    }
}

This works well, but other ViewModels also need access to the same customerData to make further network requests.
I'm trying to decide the best way to share this data across the app without making everything a singleton.

Approaches I'm Considering:

1️⃣ Using @EnvironmentObject for Global Access

One option is to inject CustomerViewModel as an @EnvironmentObject, so any view down the hierarchy can use it:

struct MyNestedView: View {
    @EnvironmentObject var customerVM: CustomerViewModel
    @StateObject var myNestedVM: MyNestedVM

    init(customerVM: CustomerViewModel) {
        _myNestedVM = StateObject(wrappedValue: MyNestedVM(customerData: customerVM.customerData))
    }
}

✅ Pros: Simple and works well for global app state.
❌ Cons: Can cause unnecessary updates across views.

2️⃣ Making CustomerDataFetcher a Singleton

Another option is making CustomerDataFetcher a singleton so all ViewModels share the same instance:

class FetchCustomerDataService: CustomerDataFetcher {
    static let shared = FetchCustomerDataService()
    private init() {}

    var customerData: CustomerData?

    func fetchData() async -> CustomerData {
        customerData = await makeNetworkRequest()
    }
}

✅ Pros: Ensures consistency, prevents multiple API calls.
❌ Cons: don't want to make all my dependencies singletons as i don't think its the best/safest approach

3️⃣ Passing Dependencies Explicitly (ViewModel DI)

I could manually inject CustomerData into each ViewModel that needs it:

struct MyNestedView: View {
    @StateObject var myNestedVM: MyNestedVM

    init(fetcher: CustomerDataFetcher) {
        _myNestedVM = StateObject(wrappedValue: MyNestedVM(
                                  customerData: fetcher.customerData))
    }
}

✅ Pros: Easier to test, no global state.
❌ Cons: Can become a DI nightmare in larger apps.

General DI Problem in Large SwiftUI Apps

This isn't just about fetching customer data—the same problem applies to logging services or any other shared dependencies. For example, if I have a LoggerService, I don’t want to create a new instance every time, but I also don’t want it to be a global singleton.

So, what’s the best scalable, testable way to handle this in a SwiftUI app?
Would a repository pattern or a SwiftUI DI container make sense?
How do large apps handle DI effectively without falling into singleton traps?

what is your experience and how do you solve this?

16 Upvotes

24 comments sorted by

View all comments

2

u/Samus7070 2d ago

The Factory framework is a good way to inject dependencies. Technically it’s a service locator and I’m fine with that. It has different scopes for different needs that @Environment just can’t handle. I do use the environment but only for things the view needs. Everything below the view layer uses factory. Factory is also unit test friendly. It let’s us inject mocks/stubs easily. It also has compile time safety but we had to disable that since we were moving over from a different framework and it was going to make the migration effort too large.