@@ -37,7 +37,7 @@ enum ViewState: Equatable {
3737}
3838
3939struct QuickPoseBasicView : View {
40- private var quickPose = QuickPose ( sdkKey: " YOUR SDK KEY " ) // register for your free key at https://dev.quickpose.ai
40+ private var quickPose = QuickPose ( sdkKey: " 01HNBD5JEKHV5X6FH2RF0X5S4N " ) // register for your free key at https://dev.quickpose.ai
4141 @EnvironmentObject var viewModel : ViewModel
4242 @EnvironmentObject var sessionConfig : SessionConfig
4343
@@ -174,11 +174,20 @@ struct QuickPoseBasicView: View {
174174
175175 . onChange ( of: state) { _ in
176176 if case . results( let result) = state {
177- let sessionDataDump = SessionDataModel ( exercise: sessionConfig. exercise. name, count: result. count, seconds: result. seconds, date: Date ( ) )
178- appendToJson ( sessionData: sessionDataDump)
177+ do {
178+ let sessionDataDump = SessionDataModel ( exercise: sessionConfig. exercise. name, count: result. count, seconds: result. seconds, date: Date ( ) )
179+ appendToJson ( sessionData: sessionDataDump)
180+ } catch {
181+ print ( " Error saving session data: \( error. localizedDescription) " )
182+ }
183+ } else {
184+ // Only update features if we're not in the results state
185+ do {
186+ quickPose. update ( features: sessionConfig. exercise. features)
187+ } catch {
188+ print ( " Error updating QuickPose features: \( error. localizedDescription) " )
189+ }
179190 }
180-
181- quickPose. update ( features: sessionConfig. exercise. features)
182191 }
183192 . onAppear ( ) {
184193 UIApplication . shared. isIdleTimerDisabled = true
@@ -245,8 +254,23 @@ struct QuickPoseBasicView: View {
245254 }
246255
247256 if hasFinished {
248- state = . results( newResults)
249- quickPose. stop ( )
257+ // Create a new SessionData object to avoid any potential reference issues
258+ let finalResults = SessionData ( count: newResults. count, seconds: newResults. seconds)
259+
260+ // First change the state, then stop QuickPose
261+ DispatchQueue . main. async {
262+ state = . results( finalResults)
263+
264+ // Add a small delay before stopping QuickPose to ensure the state change is processed
265+ DispatchQueue . main. asyncAfter ( deadline: . now( ) + 0.1 ) {
266+ // Safely stop QuickPose
267+ do {
268+ quickPose. stop ( )
269+ } catch {
270+ print ( " Error stopping QuickPose: \( error. localizedDescription) " )
271+ }
272+ }
273+ }
250274 }
251275 default :
252276 break
@@ -259,6 +283,15 @@ struct QuickPoseBasicView: View {
259283 }
260284 . onDisappear {
261285 UIApplication . shared. isIdleTimerDisabled = false
286+
287+ // Safely stop QuickPose when view disappears
288+ DispatchQueue . main. async {
289+ do {
290+ quickPose. stop ( )
291+ } catch {
292+ print ( " Error stopping QuickPose on disappear: \( error. localizedDescription) " )
293+ }
294+ }
262295 }
263296 }
264297 . navigationBarBackButtonHidden ( true )
0 commit comments