Просмотр исходного кода

Merge pull request #599 from Sjoerd-Bo3/fix/live-activity-reliability-issue-597

fix: Improve Live Activity reliability and restart mechanism
Deniz Cengiz 11 месяцев назад
Родитель
Сommit
8f8f750f1b
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)
         }