From 21e8426c09d3e790f16f910f04036f672932b389 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20V=C3=A4rbrand?= Date: Tue, 2 Sep 2025 13:30:21 +0200 Subject: [PATCH 1/3] Fix broken localUri's for attachments that do exist E.g. when rebuilding an app the system relocates the app's sandbox which gets a new path each run, which breaks the stored local uri's --- Sources/PowerSync/attachments/AttachmentQueue.swift | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/Sources/PowerSync/attachments/AttachmentQueue.swift b/Sources/PowerSync/attachments/AttachmentQueue.swift index 64c607e..bbcc143 100644 --- a/Sources/PowerSync/attachments/AttachmentQueue.swift +++ b/Sources/PowerSync/attachments/AttachmentQueue.swift @@ -463,7 +463,15 @@ public actor AttachmentQueue: AttachmentQueueProtocol { continue } - if attachment.state == AttachmentState.queuedUpload { + let newLocalUri = await getLocalUri(attachment.filename) + let newExists = try await localStorage.fileExists(filePath: newLocalUri) + if newExists { + // The file exists but the localUri is broken, lets update it. + // E.g. this happens in simulators that change the path to the app's sandbox. + updates.append(attachment.with( + localUri: newLocalUri + )) + } else if attachment.state == AttachmentState.queuedUpload { // The file must have been removed from the local storage before upload was completed updates.append(attachment.with( state: .archived, From 5b27b379bea289df8f8a62c7e8ec140acb2b33c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20V=C3=A4rbrand?= Date: Tue, 2 Sep 2025 13:34:12 +0200 Subject: [PATCH 2/3] Clear any local uri's for archived attachments I had a bunch of these, not sure if they got in that state from my experimenting/testing with broken local uri's (before arriving at the fix in the previous commit fcadf00) --- Sources/PowerSync/attachments/AttachmentQueue.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/PowerSync/attachments/AttachmentQueue.swift b/Sources/PowerSync/attachments/AttachmentQueue.swift index bbcc143..e3b7242 100644 --- a/Sources/PowerSync/attachments/AttachmentQueue.swift +++ b/Sources/PowerSync/attachments/AttachmentQueue.swift @@ -471,7 +471,7 @@ public actor AttachmentQueue: AttachmentQueueProtocol { updates.append(attachment.with( localUri: newLocalUri )) - } else if attachment.state == AttachmentState.queuedUpload { + } else if attachment.state == AttachmentState.queuedUpload || attachment.state == AttachmentState.archived { // The file must have been removed from the local storage before upload was completed updates.append(attachment.with( state: .archived, From 7c64b4763fdb7f74f4f37642ee172889cbcc74a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20V=C3=A4rbrand?= Date: Mon, 8 Sep 2025 15:47:40 +0200 Subject: [PATCH 3/3] Run verify attachments immediately when creating the AttachmentQueue To repair broken localUri's without requiring start sync, e.g. using an app before signing in/subscribing --- Sources/PowerSync/attachments/AttachmentQueue.swift | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/Sources/PowerSync/attachments/AttachmentQueue.swift b/Sources/PowerSync/attachments/AttachmentQueue.swift index e3b7242..2ab32c2 100644 --- a/Sources/PowerSync/attachments/AttachmentQueue.swift +++ b/Sources/PowerSync/attachments/AttachmentQueue.swift @@ -336,6 +336,16 @@ public actor AttachmentQueue: AttachmentQueueProtocol { errorHandler: self.errorHandler, syncThrottle: self.syncThrottleDuration ) + + Task { + do { + try await attachmentsService.withContext { context in + try await self.verifyAttachments(context: context) + } + } catch { + self.logger.error("Error verifying attachments: \(error.localizedDescription)", tag: logTag) + } + } } public func getLocalUri(_ filename: String) async -> String {