I have two SwiftData models like below:
@Model
final class SDStock {
init() {}
var id = UUID()
var title = "abc"
var code = "123456"
@Relationship(deleteRule: .cascade, inverse: \SDValue.stock)
var historyValueList: [SDValue]? = []
}
@Model
final class SDValue {
init() {}
var id = UUID()
var stock: SDStock?
var date = Date.now
var value = 0.0
}
As time grows, the app will have hundreds of stocks, and each stock will have thousands of values for historyValueList. The app should show the latest value for each stock, because SwiftData stores array as Set, so historyValueList should be sorted as per date, which caused serious performance problem when the user tap or navigate to other views.
So, is there any way to improve SwiftData sorting performance for its Set variables?
Thanks in advance!