diff --git a/SportsTime/Core/Services/ItineraryItemService.swift b/SportsTime/Core/Services/ItineraryItemService.swift index 09e36a1..befc79a 100644 --- a/SportsTime/Core/Services/ItineraryItemService.swift +++ b/SportsTime/Core/Services/ItineraryItemService.swift @@ -16,9 +16,16 @@ actor ItineraryItemService { private var debounceTask: Task? private let maxRetries = 3 + private let debounceInterval: Duration = .seconds(1.5) private init() {} + /// Cancel any pending debounce task (for cleanup) + func cancelPendingSync() { + debounceTask?.cancel() + debounceTask = nil + } + // MARK: - CRUD Operations /// Fetch all items for a trip @@ -45,16 +52,25 @@ actor ItineraryItemService { func updateItem(_ item: ItineraryItem) async { pendingUpdates[item.id] = item - // Cancel existing debounce + // Cancel existing debounce task debounceTask?.cancel() - // Start new debounce - debounceTask = Task { - try? await Task.sleep(for: .seconds(1.5)) - + // Start new debounce task with proper cancellation handling + debounceTask = Task { [weak self] in + // Check cancellation before sleeping guard !Task.isCancelled else { return } - await flushPendingUpdates() + do { + try await Task.sleep(for: self?.debounceInterval ?? .seconds(1.5)) + } catch { + // Task was cancelled during sleep + return + } + + // Check cancellation after sleeping (belt and suspenders) + guard !Task.isCancelled else { return } + + await self?.flushPendingUpdates() } }