Skip to content

Commit 2860366

Browse files
Fix broken local uri's when relaunching the app (#74)
* 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 * 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) * 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 --------- Co-authored-by: stevensJourney <[email protected]>
1 parent 7d362df commit 2860366

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

Sources/PowerSync/attachments/AttachmentQueue.swift

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,16 @@ public actor AttachmentQueue: AttachmentQueueProtocol {
336336
errorHandler: self.errorHandler,
337337
syncThrottle: self.syncThrottleDuration
338338
)
339+
340+
Task {
341+
do {
342+
try await attachmentsService.withContext { context in
343+
try await self.verifyAttachments(context: context)
344+
}
345+
} catch {
346+
self.logger.error("Error verifying attachments: \(error.localizedDescription)", tag: logTag)
347+
}
348+
}
339349
}
340350

341351
public func getLocalUri(_ filename: String) async -> String {
@@ -463,7 +473,15 @@ public actor AttachmentQueue: AttachmentQueueProtocol {
463473
continue
464474
}
465475

466-
if attachment.state == AttachmentState.queuedUpload {
476+
let newLocalUri = await getLocalUri(attachment.filename)
477+
let newExists = try await localStorage.fileExists(filePath: newLocalUri)
478+
if newExists {
479+
// The file exists but the localUri is broken, lets update it.
480+
// E.g. this happens in simulators that change the path to the app's sandbox.
481+
updates.append(attachment.with(
482+
localUri: newLocalUri
483+
))
484+
} else if attachment.state == AttachmentState.queuedUpload || attachment.state == AttachmentState.archived {
467485
// The file must have been removed from the local storage before upload was completed
468486
updates.append(attachment.with(
469487
state: .archived,

0 commit comments

Comments
 (0)