Skip to content

Commit 55f6cda

Browse files
committed
Show ticker price immediately on launch, enrich in background
1 parent ead3d47 commit 55f6cda

1 file changed

Lines changed: 30 additions & 41 deletions

File tree

Tickr/Services/StockService.swift

Lines changed: 30 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -153,48 +153,37 @@ class StockService: ObservableObject {
153153
let exchange = meta.fullExchangeName ?? meta.exchangeName ?? ""
154154

155155

156-
// Fetch enrichment data (sector + market cap) in parallel, then build quote
157-
let enrichGroup = DispatchGroup()
158-
var sector: String? = self?.sectorCache[symbol]?.sector
159-
var industry: String? = self?.sectorCache[symbol]?.industry
160-
var marketCap: String? = self?.marketCapCache[symbol]
161-
162-
if sector == nil {
163-
enrichGroup.enter()
164-
self?.fetchSectorInfo(symbol: symbol) { s, i in
165-
sector = s; industry = i
166-
enrichGroup.leave()
167-
}
156+
// Return quote immediately with cached enrichment data
157+
// Enrichment fetches in background — will be available on next refresh
158+
let cachedSector = self?.sectorCache[symbol]
159+
let cachedMCap = self?.marketCapCache[symbol]
160+
161+
let quote = StockQuote(
162+
symbol: meta.symbol,
163+
companyName: meta.longName ?? meta.shortName ?? meta.symbol,
164+
price: price,
165+
change: change,
166+
changePercent: changePercent,
167+
previousClose: previousClose,
168+
dayHigh: meta.regularMarketDayHigh,
169+
dayLow: meta.regularMarketDayLow,
170+
volume: meta.regularMarketVolume,
171+
fiftyTwoWeekHigh: meta.fiftyTwoWeekHigh,
172+
fiftyTwoWeekLow: meta.fiftyTwoWeekLow,
173+
currency: meta.currency ?? "USD",
174+
exchange: exchange,
175+
sector: cachedSector?.sector,
176+
industry: cachedSector?.industry,
177+
marketCap: cachedMCap
178+
)
179+
completion(.success(quote))
180+
181+
// Fetch enrichment in background (cached for next refresh)
182+
if cachedSector == nil {
183+
self?.fetchSectorInfo(symbol: symbol) { _, _ in }
168184
}
169-
170-
if marketCap == nil {
171-
enrichGroup.enter()
172-
self?.fetchMarketCap(symbol: symbol, exchange: exchange) { mc in
173-
marketCap = mc
174-
enrichGroup.leave()
175-
}
176-
}
177-
178-
enrichGroup.notify(queue: .global()) {
179-
let quote = StockQuote(
180-
symbol: meta.symbol,
181-
companyName: meta.longName ?? meta.shortName ?? meta.symbol,
182-
price: price,
183-
change: change,
184-
changePercent: changePercent,
185-
previousClose: previousClose,
186-
dayHigh: meta.regularMarketDayHigh,
187-
dayLow: meta.regularMarketDayLow,
188-
volume: meta.regularMarketVolume,
189-
fiftyTwoWeekHigh: meta.fiftyTwoWeekHigh,
190-
fiftyTwoWeekLow: meta.fiftyTwoWeekLow,
191-
currency: meta.currency ?? "USD",
192-
exchange: exchange,
193-
sector: sector,
194-
industry: industry,
195-
marketCap: marketCap
196-
)
197-
completion(.success(quote))
185+
if cachedMCap == nil {
186+
self?.fetchMarketCap(symbol: symbol, exchange: exchange) { _ in }
198187
}
199188
} catch {
200189
completion(.failure(error))

0 commit comments

Comments
 (0)