05/08 Updated code: fix some issues
This commit is contained in:
@@ -169,7 +169,24 @@ class SyncManager: ObservableObject {
|
||||
.filter { $0.syncStatus == "pending" }
|
||||
.sorted { $0.createdAt < $1.createdAt }
|
||||
|
||||
// Pre-fetch all LocalInspections once for the parent-failed guard below.
|
||||
let allInspections = (try? context.fetch(FetchDescriptor<LocalInspection>())) ?? []
|
||||
|
||||
for issue in pending {
|
||||
// Guard: if the parent inspection permanently failed to sync, submitting
|
||||
// this issue without an inspection_id would create an orphaned server
|
||||
// record. Skip the issue and mark it failed so the inspector can see
|
||||
// the error rather than losing the association silently.
|
||||
if let parentInspection = allInspections.first(where: {
|
||||
$0.localId == issue.inspectionLocalId
|
||||
}), parentInspection.syncStatus == "failed" {
|
||||
issue.syncStatus = "failed"
|
||||
issue.syncErrorMessage = "Parent inspection failed to sync — issue cannot be submitted."
|
||||
try? context.save()
|
||||
syncError = "Issue \(issue.localId.prefix(8))\u{2026} blocked: parent inspection did not sync."
|
||||
continue
|
||||
}
|
||||
|
||||
do {
|
||||
let issueId = try await APIClient.shared.submitIssue(issue)
|
||||
issue.serverId = issueId
|
||||
|
||||
Reference in New Issue
Block a user