05/16 Fix bugs 2
This commit is contained in:
@@ -36,6 +36,22 @@ class SyncManager: ObservableObject {
|
||||
private var pollTask: Task<Void, Never>? // replaces Timer — Task.sleep works correctly
|
||||
private let pollInterval: UInt64 = 60_000_000_000 // 60 seconds in nanoseconds
|
||||
|
||||
// ── Shared date formatters ────────────────────────────────────────────
|
||||
// DateFormatter init is expensive — allocating one per poll call or per
|
||||
// issue would add measurable overhead at sync time. These are created
|
||||
// once and reused across all calls. Both are nonisolated statics so they
|
||||
// can be read from any context without actor-hopping.
|
||||
//
|
||||
// isoFormatter — parses/formats ISO 8601 strings from the server API
|
||||
// e.g. "2026-05-01T14:30:00"
|
||||
// notifFormatter — same format, used to advance the notification poll cursor
|
||||
nonisolated static let isoFormatter: DateFormatter = {
|
||||
let f = DateFormatter()
|
||||
f.locale = Locale(identifier: "en_US_POSIX")
|
||||
f.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
|
||||
return f
|
||||
}()
|
||||
|
||||
static let shared = SyncManager()
|
||||
private init() {}
|
||||
|
||||
@@ -106,9 +122,7 @@ class SyncManager: ObservableObject {
|
||||
}
|
||||
|
||||
// Update the cursor to the newest notification's timestamp
|
||||
let fmt = DateFormatter()
|
||||
fmt.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
|
||||
let dates = notifications.compactMap { fmt.date(from: $0.createdAt) }
|
||||
let dates = notifications.compactMap { Self.isoFormatter.date(from: $0.createdAt) }
|
||||
if let newest = dates.max() {
|
||||
lastNotificationFetch = newest
|
||||
}
|
||||
@@ -424,8 +438,6 @@ class SyncManager: ObservableObject {
|
||||
if let sid = local.serverId { serverIdMap[sid] = local }
|
||||
}
|
||||
|
||||
let isoFmt = ISO8601DateFormatter()
|
||||
|
||||
for api in apiIssues {
|
||||
if let existing = serverIdMap[api.id] {
|
||||
// Update mutable fields on existing record
|
||||
@@ -455,11 +467,7 @@ class SyncManager: ObservableObject {
|
||||
serverPaths.append(contentsOf: api.resultPhotos)
|
||||
local.photoServerPaths = serverPaths
|
||||
if let ts = api.reportedAt,
|
||||
let date = isoFmt.date(from: ts) ?? {
|
||||
let f = DateFormatter()
|
||||
f.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
|
||||
return f.date(from: ts)
|
||||
}() {
|
||||
let date = Self.isoFormatter.date(from: ts) {
|
||||
local.createdAt = date
|
||||
}
|
||||
context.insert(local)
|
||||
|
||||
Reference in New Issue
Block a user