I'm working on a companion Watch App for my iOS App.
.onAppear() and .onDisappear() do not run if they're inside a child view of the three main views in my content view. For example, my first page is a list of workouts, If I click and navigate to a workout on the list the .onAppear() on that workout page does not run. Is this a bug on WatchOS 8 beta 4 or am I doing something wrong? The iOS app has very similar functionality and the .onAppear/.onDisappear all work fine on iOS 15 beta 4. Lastly, they seem to work in previews, just not in simulators or my device.
The content view for the app (Those 3 functions do run):
import SwiftUI
import CoreData
struct ContentView: View {
@Environment(\.managedObjectContext) private var viewContext
var body: some View {
TabView {
WorkoutList()
ExerciseList()
HeartRate()
}
.onAppear {
loadDefaultExercises()
loadDefaultWorkouts()
loadProfile()
}
}
func loadDefaultExercises() -> () {... }
func loadDefaultExercises() -> () {... }
func loadDefaultExercises() -> () {... }
}
The WorkoutList() view (.onAppear/.onDisappears placed in here do run but only once at app launch):
import SwiftUI
struct WorkoutList: View {
@Environment(\.managedObjectContext) private var viewContext
@FetchRequest(
sortDescriptors: [NSSortDescriptor(keyPath: \Workout.name, ascending: true)],
animation: .default)
var workouts: FetchedResults<Workout>
var body: some View {
NavigationView {
List {
ForEach(workouts) { workout in
NavigationLink(destination: WorkoutDetail(workout: workout)) {
WorkoutListRow(workout: workout)
}
}
}
.navigationTitle("Workouts")
.onAppear {
print("On Appear (Working but only once on launch)")
}
}
}
}
The WorkoutDetail page that appears when I click on a workout from WorkoutList() (.onAppear/.onDisappears placed in here on any child views from here do not run):
import SwiftUI
import CoreData
struct WorkoutDetail: View {
@ObservedObject var workout: Workout
var body: some View {
ScrollView {
VStack(alignment: .leading) {... }
}
.onAppear {
print("On Appear (NOT WORKING)")
}
.navigationTitle(workout.name ?? "")
}
}
Thanks in advance.