Преглед на файлове

fix: Improve Live Activity reliability and restart mechanism

- Add 1-second delay after ending activities for proper iOS cleanup
- Replace recursive pushUpdate with direct activity.update to avoid race conditions
- Add error recovery by resetting currentActivity on creation failure

Addresses sandbox extension errors and prevents Live Activities from
disappearing after restart attempts.

Fixes #597
Sjoerd Bozon преди 11 месеца
родител
ревизия
fc716a28d0
променени са 1 файла, в които са добавени 14 реда и са изтрити 1 реда
  1. 14 1
      Trio/Sources/Services/LiveActivity/LiveActivityManager.swift

+ 14 - 1
Trio/Sources/Services/LiveActivity/LiveActivityManager.swift

@@ -360,12 +360,21 @@ final class LiveActivityManager: Injectable, ObservableObject, SettingsObserver
                 )
                 currentActivity = ActiveActivity(activity: activity, startDate: Date.now)
                 debug(.default, "[LiveActivityManager] Created new activity: \(activity.id)")
-                await pushUpdate(state)
+
+                // Update the newly created activity with actual data
+                let updateContent = ActivityContent(
+                    state: state,
+                    staleDate: Date.now.addingTimeInterval(5 * 60)
+                )
+                await activity.update(updateContent)
+                debug(.default, "[LiveActivityManager] Updated new activity with actual data")
             } catch {
                 debug(
                     .default,
                     "\(#file): Error creating new activity: \(error)"
                 )
+                // Reset currentActivity on error to allow retry on next update
+                currentActivity = nil
             }
         }
     }
@@ -428,6 +437,10 @@ final class LiveActivityManager: Injectable, ObservableObject, SettingsObserver
             try? await Task.sleep(nanoseconds: 200_000_000) // 0.2s sleep
         }
 
+        // Add additional delay to ensure iOS has fully cleaned up the previous activity
+        debug(.default, "Waiting additional time for iOS to clean up...")
+        try? await Task.sleep(nanoseconds: 1_000_000_000) // 1s additional delay
+
         Task { @MainActor in
             await self.pushUpdate(contentState)
         }