'Wait for config files to load in SwiftUI App

I have an app that is pure SwiftUI. I am trying to stick to dependency injection for my Home view and its associated ViewModel.

In that spirit, there are some elements of a config file which are a dependency of my Home views associated ViewModel. Mainly, the current list of words for the speech recognition service to look for.

I would love to call my config remotely with a very short timeout (less than two seconds) and provide a local copy in the event of timeout.

For example:

struct MyApp: App {

    var words: SpeechDictionary
    var viewModel: HomeViewModel
    
    init() {
        words = SpeechDictionary()
        //Any blocking function, await used as illustration
        await words.update()
        
        viewModel = HomeViewModel(dictionary: words)
    }
    
    var body: some Scene {
        WindowGroup {
            HomeView(viewModel: viewModel)
        }
    }
}

If I don't await the calls here, I have to add state flags, subscribers, and hope the user doesn't try to use the app before the home screen loads (or lock them out with a second spinner). This all seems very un-swifty and not following SOLID patterns.

What's the cleanest way to handle the above situation?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source