I have built an app in SwiftUI and have encountered an annoying issue.
When the app is presented the app shows a custom launch screen while the data of the app loads asynchronously. Once finished, the app's main view is presented to the user. Once the user reaches this point, as soon as they drag the app back into he background the navigation stack pops back to the first launch screen for no reason.
Here are the relevant code snippets:
MyApp.swift
@main
struct Jam500App: App {
@Environment(\.scenePhase) var lifecycle
init() {
//initial setup goes here
}
var body: some Scene {
WindowGroup {
LaunchView()
}
}
}
LaunchView.swift
struct LaunchView: View {
@ObservedObject var control: LaunchViewControl = LaunchViewControl()
var body: some View {
NavigationView(content: {
ZStack {
//some view hierarchy
NavigationLink(destination: RootView(),
isActive: $control.isReady,
label: {EmptyView()})
}
.onAppear(perform: {
control.loadData()
})
.navigationBarTitle("")
.navigationBarHidden(true)
.statusBar(hidden: true)
}).navigationViewStyle(StackNavigationViewStyle())
}
}
LaunchViewControl.swift
final class LaunchViewControl: ObservableObject {
@Published var isReady: Bool = false
private var executing: Bool = false
private let dataService = DataService.shared
func loadData() {
//runs all initial app setup needed before the app starts (asynchronously)
guard !executing else {
return
}
executing = true
self.setupCoreData()
}
private func setupCoreData() {
//Initialises CoreData with CloudKit (asynchronously)
CloudCoreDataService.shared.setup { [weak self] in
guard let self = self else { return }
self.checkRequiresInitialData()
}
}
private func checkRequiresInitialData() {
self.dataService.requiresInitialDataSetup(onCompletion: { [weak self] (isRequired) in
guard let self = self else { return }
if isRequired {
self.setupInitialData()
} else {
self.isReady = true
}
})
}
private func setupInitialData() {
//Generates initial database if needed
self.dataService.setupInitialData { [weak self] (_) in
guard let self = self else { return }
self.isReady = true
}
}
}
RootView.swift
struct RootView: View {
@ObservedObject var control = RootViewControl()
var body: some View {
HStack(spacing: 0) {
//some view hierarchy
}
.navigationBarTitle("")
.navigationBarHidden(true)
.statusBar(hidden: true)
}
}
Has anyone else encountered this issue with Xcode 12.4?