@@ -41,6 +41,12 @@ actor SQLiteHealthArchiveStore: HealthArchiveStore {
|
||
| 41 | 41 |
private let databaseURL: URL |
| 42 | 42 |
private var didPrepareSchema = false |
| 43 | 43 |
|
| 44 |
+ nonisolated private static let monitoredTypeMetadataByIdentifier: [String: (displayName: String, category: String)] = {
|
|
| 45 |
+ Dictionary(uniqueKeysWithValues: HealthKitService.allTypes.map {
|
|
| 46 |
+ ($0.id, (displayName: $0.displayName, category: $0.category.rawValue)) |
|
| 47 |
+ }) |
|
| 48 |
+ }() |
|
| 49 |
+ |
|
| 44 | 50 |
init(databaseURL: URL? = nil) {
|
| 45 | 51 |
self.databaseURL = databaseURL ?? Self.defaultDatabaseURL |
| 46 | 52 |
} |
@@ -2378,10 +2384,11 @@ actor SQLiteHealthArchiveStore: HealthArchiveStore {
|
||
| 2378 | 2384 |
|
| 2379 | 2385 |
var stepResult = sqlite3_step(statement) |
| 2380 | 2386 |
while stepResult == SQLITE_ROW {
|
| 2387 |
+ let typeIdentifier = columnText(statement, 1) ?? "" |
|
| 2381 | 2388 |
rows.append(CachedArchiveTypeSummary( |
| 2382 | 2389 |
observationID: sqlite3_column_int64(statement, 0), |
| 2383 |
- sampleTypeIdentifier: columnText(statement, 1) ?? "", |
|
| 2384 |
- displayName: columnText(statement, 2), |
|
| 2390 |
+ sampleTypeIdentifier: typeIdentifier, |
|
| 2391 |
+ displayName: columnText(statement, 2) ?? Self.monitoredTypeMetadataByIdentifier[typeIdentifier]?.displayName, |
|
| 2385 | 2392 |
visibleRecordCount: columnInt(statement, 3) ?? 0, |
| 2386 | 2393 |
appearedCount: columnInt(statement, 4) ?? 0, |
| 2387 | 2394 |
disappearedCount: columnInt(statement, 5) ?? 0, |
@@ -2541,12 +2548,21 @@ actor SQLiteHealthArchiveStore: HealthArchiveStore {
|
||
| 2541 | 2548 |
if let cached = lookupCache?.sampleTypeIDs[typeIdentifier] {
|
| 2542 | 2549 |
return cached |
| 2543 | 2550 |
} |
| 2551 |
+ let metadata = Self.monitoredTypeMetadataByIdentifier[typeIdentifier] |
|
| 2544 | 2552 |
try withStatement( |
| 2545 |
- "INSERT OR IGNORE INTO sample_types (type_identifier, display_name, category) VALUES (?, NULL, NULL)", |
|
| 2553 |
+ """ |
|
| 2554 |
+ INSERT INTO sample_types (type_identifier, display_name, category) |
|
| 2555 |
+ VALUES (?, ?, ?) |
|
| 2556 |
+ ON CONFLICT(type_identifier) DO UPDATE SET |
|
| 2557 |
+ display_name = COALESCE(sample_types.display_name, excluded.display_name), |
|
| 2558 |
+ category = COALESCE(sample_types.category, excluded.category) |
|
| 2559 |
+ """, |
|
| 2546 | 2560 |
db: db, |
| 2547 | 2561 |
statementCache: statementCache |
| 2548 | 2562 |
) { statement in
|
| 2549 | 2563 |
bindText(typeIdentifier, to: 1, in: statement) |
| 2564 |
+ bindText(metadata?.displayName, to: 2, in: statement) |
|
| 2565 |
+ bindText(metadata?.category, to: 3, in: statement) |
|
| 2550 | 2566 |
guard sqlite3_step(statement) == SQLITE_DONE else {
|
| 2551 | 2567 |
throw SQLiteHealthArchiveStoreError.stepFailed(lastErrorMessage(db)) |
| 2552 | 2568 |
} |