1 year ago
#384503
serlingpa
RealmSwift — updating UI takes forever
I have a view on a collection of RecentTrades
objects that are populated by a background task fetching using a URLSession
.
My view looks like this:
struct StatusView: View {
@ObservedResults(RecentTrades.self) var recentTrades
func tradeCountColumn(x: RecentTrades) -> some View {
Text(String(x.trades.count))
}
func isFetchingColumn(recentTrades: RecentTrades) -> some View {
recentTrades.isFetching ? AnyView(ProgressView()) : AnyView(Text("Loaded"))
}
var body: some View {
Table(recentTrades) {
TableColumn("Pair", value: \.name)
TableColumn("Trade count", content: tradeCountColumn)
TableColumn("Loaded", content: isFetchingColumn)
}
}
}
My RecentTrades
model looks like this:
class RecentTrades: Object, ObjectKeyIdentifiable {
@Persisted(primaryKey: true) var name: String
@Persisted var last: String
@Persisted var trades: List<Trade>
@Persisted var isFetching: Bool
}
class Trade: EmbeddedObject {
@Persisted var price: String
@Persisted var volume: String
@Persisted(indexed: true) var time: Date
@Persisted var buySell: BuySell
@Persisted var marketLimit: MarketLimit
@Persisted var miscellaneous: String
}
Unfortunately, this is super-slow. There are a number of Task
s fetching trades using a URLSession
for each pair of coins, but it is only when this view is open that the application grinds to a crawl in terms of responsiveness. I am really interested only in the isFetching
property and the count of the trades
List
on my RecentTrades
model.
There are around 50 or so RecentTrades
objects in my Realm, and some of those have up to 1,000,000 Trade
s in the trades
property.
Is there a way to improve the efficiency of this view, so that I don't have to fetch the entire collection when any of the properties of the objects inside change? I know I an set up a change listener, but is this going to be any better? Will this still not deliver all of the objects, or does this method fetch lazily so that I can count
without having to fetch all of the objects? Why does the ObservedResults
not behave like this too?
swift
swiftui
realm
0 Answers
Your Answer