feat(sync): update CloudKit sync methods to use modificationDate delta
- fetchStadiumsForSync now accepts since: Date? parameter - fetchTeamsForSync changed from per-sport to all teams with delta sync - fetchGamesForSync uses modificationDate instead of game dateTime When lastSync is nil, fetches all records (first sync). When lastSync has value, fetches only modified records (delta sync). Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -213,9 +213,15 @@ actor CloudKitService {
|
|||||||
|
|
||||||
// MARK: - Sync Fetch Methods (return canonical IDs directly from CloudKit)
|
// MARK: - Sync Fetch Methods (return canonical IDs directly from CloudKit)
|
||||||
|
|
||||||
/// Fetch stadiums with canonical IDs for sync operations
|
/// Fetch stadiums for sync operations
|
||||||
func fetchStadiumsForSync() async throws -> [SyncStadium] {
|
/// - Parameter lastSync: If nil, fetches all stadiums. If provided, fetches only stadiums modified since that date.
|
||||||
let predicate = NSPredicate(value: true)
|
func fetchStadiumsForSync(since lastSync: Date?) async throws -> [SyncStadium] {
|
||||||
|
let predicate: NSPredicate
|
||||||
|
if let lastSync = lastSync {
|
||||||
|
predicate = NSPredicate(format: "modificationDate >= %@", lastSync as NSDate)
|
||||||
|
} else {
|
||||||
|
predicate = NSPredicate(value: true)
|
||||||
|
}
|
||||||
let query = CKQuery(recordType: CKRecordType.stadium, predicate: predicate)
|
let query = CKQuery(recordType: CKRecordType.stadium, predicate: predicate)
|
||||||
|
|
||||||
let (results, _) = try await publicDatabase.records(matching: query)
|
let (results, _) = try await publicDatabase.records(matching: query)
|
||||||
@@ -230,9 +236,15 @@ actor CloudKitService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Fetch teams with canonical IDs for sync operations
|
/// Fetch teams for sync operations
|
||||||
func fetchTeamsForSync(for sport: Sport) async throws -> [SyncTeam] {
|
/// - Parameter lastSync: If nil, fetches all teams. If provided, fetches only teams modified since that date.
|
||||||
let predicate = NSPredicate(format: "sport == %@", sport.rawValue)
|
func fetchTeamsForSync(since lastSync: Date?) async throws -> [SyncTeam] {
|
||||||
|
let predicate: NSPredicate
|
||||||
|
if let lastSync = lastSync {
|
||||||
|
predicate = NSPredicate(format: "modificationDate >= %@", lastSync as NSDate)
|
||||||
|
} else {
|
||||||
|
predicate = NSPredicate(value: true)
|
||||||
|
}
|
||||||
let query = CKQuery(recordType: CKRecordType.team, predicate: predicate)
|
let query = CKQuery(recordType: CKRecordType.team, predicate: predicate)
|
||||||
|
|
||||||
let (results, _) = try await publicDatabase.records(matching: query)
|
let (results, _) = try await publicDatabase.records(matching: query)
|
||||||
@@ -248,37 +260,29 @@ actor CloudKitService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Fetch games with canonical IDs for sync operations
|
/// Fetch games for sync operations
|
||||||
func fetchGamesForSync(
|
/// - Parameter lastSync: If nil, fetches all games. If provided, fetches only games modified since that date.
|
||||||
sports: Set<Sport>,
|
func fetchGamesForSync(since lastSync: Date?) async throws -> [SyncGame] {
|
||||||
startDate: Date,
|
let predicate: NSPredicate
|
||||||
endDate: Date
|
if let lastSync = lastSync {
|
||||||
) async throws -> [SyncGame] {
|
predicate = NSPredicate(format: "modificationDate >= %@", lastSync as NSDate)
|
||||||
var allGames: [SyncGame] = []
|
} else {
|
||||||
|
predicate = NSPredicate(value: true)
|
||||||
for sport in sports {
|
}
|
||||||
let predicate = NSPredicate(
|
|
||||||
format: "sport == %@ AND dateTime >= %@ AND dateTime <= %@",
|
|
||||||
sport.rawValue,
|
|
||||||
startDate as NSDate,
|
|
||||||
endDate as NSDate
|
|
||||||
)
|
|
||||||
let query = CKQuery(recordType: CKRecordType.game, predicate: predicate)
|
let query = CKQuery(recordType: CKRecordType.game, predicate: predicate)
|
||||||
|
|
||||||
let (results, _) = try await publicDatabase.records(matching: query)
|
let (results, _) = try await publicDatabase.records(matching: query)
|
||||||
|
|
||||||
let games = results.compactMap { result -> SyncGame? in
|
return results.compactMap { result -> SyncGame? in
|
||||||
guard case .success(let record) = result.1 else { return nil }
|
guard case .success(let record) = result.1 else { return nil }
|
||||||
let ckGame = CKGame(record: record)
|
let ckGame = CKGame(record: record)
|
||||||
|
|
||||||
// Extract canonical IDs directly from CloudKit
|
|
||||||
guard let canonicalId = ckGame.canonicalId,
|
guard let canonicalId = ckGame.canonicalId,
|
||||||
let homeTeamCanonicalId = ckGame.homeTeamCanonicalId,
|
let homeTeamCanonicalId = ckGame.homeTeamCanonicalId,
|
||||||
let awayTeamCanonicalId = ckGame.awayTeamCanonicalId,
|
let awayTeamCanonicalId = ckGame.awayTeamCanonicalId,
|
||||||
let stadiumCanonicalId = ckGame.stadiumCanonicalId
|
let stadiumCanonicalId = ckGame.stadiumCanonicalId
|
||||||
else { return nil }
|
else { return nil }
|
||||||
|
|
||||||
// For the Game domain object, use canonical IDs directly
|
|
||||||
guard let game = ckGame.game(
|
guard let game = ckGame.game(
|
||||||
homeTeamId: homeTeamCanonicalId,
|
homeTeamId: homeTeamCanonicalId,
|
||||||
awayTeamId: awayTeamCanonicalId,
|
awayTeamId: awayTeamCanonicalId,
|
||||||
@@ -292,12 +296,7 @@ actor CloudKitService {
|
|||||||
awayTeamCanonicalId: awayTeamCanonicalId,
|
awayTeamCanonicalId: awayTeamCanonicalId,
|
||||||
stadiumCanonicalId: stadiumCanonicalId
|
stadiumCanonicalId: stadiumCanonicalId
|
||||||
)
|
)
|
||||||
}
|
}.sorted { $0.game.dateTime < $1.game.dateTime }
|
||||||
|
|
||||||
allGames.append(contentsOf: games)
|
|
||||||
}
|
|
||||||
|
|
||||||
return allGames.sorted { $0.game.dateTime < $1.game.dateTime }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// MARK: - League Structure & Team Aliases
|
// MARK: - League Structure & Team Aliases
|
||||||
|
|||||||
Reference in New Issue
Block a user