My understanding is a CurrentValueSubject publisher in Combine is good for accessing on demand, as opposed to a regular Publisher that emits a value once. So I'm trying to use one here in an Environment Object to store the total energy burned in an HKWorkout so that I can access it after the workout is finished in a SwiftUI View. With the code below I get the compiler error Cannot convert return expression of type 'AnyCancellable' to return type 'Double'
so I think I need to do some type of casting but can't figure it out?
class WorkoutManager: NSObject, HKWorkoutSessionDelegate, HKLiveWorkoutBuilderDelegate, ObservableObject {
var finishedWorkoutTotalEnergyBurned = CurrentValueSubject<Double, Never>(0.0)
func stopWorkout() {
self.finishedWorkoutTotalEnergyBurned.value = unwrappedWorkout.totalEnergyBurned!.doubleValue(for: .kilocalorie())
}
}
struct SummaryView: View {
@StateObject var workoutManager = WorkoutManager()
var body: some View {
Text("\(getFinishedWorkoutTotalEnergyBurned())")
.navigationBarHidden(true)
//.navigationTitle("Workout Complete")
}
func getFinishedWorkoutTotalEnergyBurned() -> Double {
workoutManager.finishedWorkoutTotalEnergyBurned.sink(receiveValue: { $0 })
}
}