What does @main generate for SwiftUI.

All the docs say that @main generates a main() method for SwiftUI, replacing the need for a main.swift. I can't find a reference for what is actually generated. I'd like to override the initial bootstrapping of the app during unit testing for code coverage purposes. The way we've done this in the past uses UIApplicationMain to dynamically control the app delegate in main.swift.

I've looked through the headers, but the App class just has a declaration for main() but the implementation is not available.

How can I do something similar with SwiftUI. Is there an analogous call to UIApplicationMain?

Replies

I had a similar question. I've since concluded that my mental model for SwiftUI application development needed to change.

I now create a @StateObject in my @main struct / class. The initializer for this class performs any safe and quick initialization and then dispatches the remaining tasks to a background thread. The object will then publish (see Combine) a completion result. I subscribe to the object in a later view and deal with any failures there.

Essentially, initialization should be though of as a piece of data in the application data model. The application proceeds with creating views and, possibly, interacting with the user until that information is needed. At that point, a progress bar or spinner may be necessary until it is available. Then either present the error to the user, kick off a fallback, or proceed with the application as appropriate.