I have a list of transactions, each with certain properties but the ones I'm interested in are the dateParsed: String, and a signedAmount: Double, so I can group the transactions by month and created a chart.
I have a TransactionListViewModel with the following code (that I have trimmed in order to try to debug it and make it work):
import Foundation
import Collections
typealias TransactionGroup = OrderedDictionary<String, [Transaction]>
typealias TransactionPrefixSum = [(String, Double)]
@Observable final class TransactionListViewModel {
var transactions: [Transaction] = []
init() {
Task {
transactions = try await getTransactions()
}
}
@MainActor
func getTransactions() async throws -> [Transaction] {
let data = ... // fetch a JSON from the network with a list of Transaction
return try JSONDecoder().decode([Transaction].self, from: data)
} catch {
print("Error: ", error)
return []
}
}
func accumulateTransactions() async -> TransactionPrefixSum {
let today = "02/17/2022".dateParsed()
let dateInterval = Calendar.current.dateInterval(of: .month, for: today)!
var sum: Double = .zero
var cumulativeSum = TransactionPrefixSum()
return cumulativeSum
}
}
And I'm trying to call this accumulateTransactions() method in my ContentView:
import SwiftUI
import Charts
struct ContentView: View {
@Environment(TransactionListViewModel.self)
var transactionListViewModel: TransactionListViewModel
var body: some View {
NavigationStack {
ScrollView {
VStack(alignment: .leading, spacing: 24) {
Text("Overview")
.font(.title2)
.bold()
let data = transactionListViewModel.accumulateTransactions()
let totalExpenses = data.last?.1 ?? 0
Chart(data, id: \.key) { key, value in
LineMark(
x: .value(key, value.dateParsed),
y: .value(totalExpenses, value.signedAmount)
)
}
RecentTransactionList()
}
}
}
}
}
However doing this, I just get an error:
The compiler is unable to type-check this expression in reasonable time; try breaking up the expression into distinct sub-expressions and the app can’t be built.
I created a Swift Playground project to test this, and it looks removing the SwiftUI part, it works fine as I see the result when I'm trying to print the content of transactionsViewModel.accumulateTransactions()
I'm not sure what the problem is, perhaps my use of the async/await?
You have an async call right in the body:
It would not compile.
Update as follows: