The 2024 WWDC video 'Meet FinanceKit' has a code example to get the latest 7 AccountBalance entries for a given account:
// Get latest 7 available balances for account
func getBalances(account: Account) async throws -> [AccountBalance] {
let sortDescriptor = SortDescriptor(\AccountBalance.asOfDate, order: .reverse)
let predicate = #Predicate<AccountBalance> { balance in
balance.available != nil &&
balance.accountId == account.id
}
let query = AccountBalanceQuery(
sortDescriptors: [sortDescriptor],
predicate: predicate,
limit: 7
)
return try await store.accountBalances(query: query).reversed()
}
This code does not compile, because the AccountBalance struct has no field named asOfDate
- you need to inspect the currentBalance
enum, switch over the enum type value, and then extract asOfDate
from the associated value.
All of this can't be done in a KeyPath (as far as I know 🤷♂️), which I think means it's impossible to get recent balances, without specifying a date, or getting all balances, and then sorting them in memory.
Am I missing something? I'd love to be proven wrong :)
FB14076698