This commit is contained in:
2025-07-15 16:33:55 -04:00
parent 39fd45e03f
commit 48bbbbf692
8 changed files with 242 additions and 85 deletions

View File

@ -10,18 +10,32 @@ struct CloudKitSyncObserver: ViewModifier {
content
.id(refreshID) // Force view refresh when this changes
.onReceive(NotificationCenter.default.publisher(for: .cloudKitDataDidChange)) { _ in
// When we receive a notification that CloudKit data changed:
// 1. Create a new UUID to force view refresh
refreshID = UUID()
// 2. Optionally, you can also manually refresh the model context
// This is sometimes needed for complex relationships
Task { @MainActor in
try? modelContext.fetch(FetchDescriptor<Exercise>())
// Add other model types as needed
// do {
// for entity in modelContext.container.schema.entities {
// fetchAll<entity.Type>(of: entity.Type, from: modelContext)
// }
// } catch {
// print("ERROR: failed to fetch data on CloudKit change")
// }
//
// try? modelContext.fetch(FetchDescriptor<Exercise>())
// try? modelContext.fetch(FetchDescriptor<ExerciseType>())
// try? modelContext.fetch(FetchDescriptor<Muscle>())
// try? modelContext.fetch(FetchDescriptor<MuscleGroup>())
// try? modelContext.fetch(FetchDescriptor<Exercise>())
// try? modelContext.fetch(FetchDescriptor<Exercise>())
// try? modelContext.fetch(FetchDescriptor<Exercise>())
// try? modelContext.fetch(FetchDescriptor<Exercise>())
// TODO: add more entities?
}
}
}
private func fetchAll<T: PersistentModel>(of type: T.Type,from modelContext: ModelContext) async throws -> [T]? {
try modelContext.fetch(FetchDescriptor<T>())
}
}
// Extension to make it easier to use the modifier