I'm also experiencing this issue, but I'm not using Charts. It's when navigating back to my Home tab on macOS only. This works fine on iOS.
import SwiftData
import SwiftUI
struct ContentView: View {
@Environment(\.modelContext) private var modelContext
@Query private var publications: [Publication]
@State private var publicationError: EncodedPublicationError?
@State private var showErrorAlert = false
@State private var selectedTab = 0
var body: some View {
NavigationStack {
TabView(selection: $selectedTab) {
ScrollView {
FeaturedView()
RecentlyOpenedView()
Spacer(minLength: 20)
RecentlyUpdatedView()
Spacer(minLength: 60)
}
.tag(0)
.tabItem {
Label("Home", systemImage: "house.fill")
}
LibraryView()
.tag(1)
.tabItem {
Label("Library", systemImage: "books.vertical.fill")
}
QuestionView()
.tag(2)
.tabItem {
Label("Answers", systemImage: "text.bubble.fill")
}
SearchView()
.tag(3)
.tabItem {
Label("Search", systemImage: "magnifyingglass")
}
}
.navigationTitle(navigationTitle)
.tabViewStyle(.sidebarAdaptable)
.refreshable { await refreshPublications() }
}
.overlay {
if publications.isEmpty {
initialSetupView
}
}
.alert(isPresented: $showErrorAlert, error: publicationError) {
Button("OK", role: .cancel) { }
}
.task {
if publications.isEmpty {
await refreshPublications()
}
}
}
private var navigationTitle: String {
switch selectedTab {
case 0: return "Home"
case 1: return "Library"
case 2: return "Answers"
case 3: return "Search"
default: return ""
}
}
@ViewBuilder
private var initialSetupView: some View {
Color.primary
.colorInvert()
.overlay {
ProgressView {
Text("Loading Publications…")
}
}
.ignoresSafeArea()
.transition(.opacity)
}
// MARK: - Data Refresh
@MainActor
private func refreshPublications() async {
let importer = PublicationImporter(modelContainer: modelContext.container)
do {
try await importer.backgroundInsert()
} catch {
self.publicationError = error as? EncodedPublicationError
self.showErrorAlert = true
}
}
}
Topic:
UI Frameworks
SubTopic:
SwiftUI
Tags: