admin管理员组文章数量:1336632
I have a SwiftData @Query which gives me a array of Event objects. Due to the nature of the object (e.g. dynamic distance calculations) I filter the events manually using function.
This works fine for any changes in the objects, and they are shown directly in the SwiftUI List. But If I add an object the List is not updated (as it is created from the filtered list, not directly from the @Quary).
Is there a way to subscribe to the @Quary to get a notification (or to pass the state on) so the data is refreshed? This is the relevant code, the data variable contains the data sorted by day and filtered using a normal function. FilterManager is an observable object, which notifies if the filter parameters change.
@State var data: [Date:[Event]] = [:]
@Query(sort: \Event.startTime) private var events: [Event]
List {
ForEach(Array(data.keys).sorted(by: <), id: \.self) { day in
Section(header: Text(day,format: Date.FormatStyle()
.year(.defaultDigits)
.month(.abbreviated)
.day(.twoDigits)
.weekday(.abbreviated)).font(.footnote)) {
ForEach(data[day]!) { item in
[...]
}
}
}.onAppear(){
data = filterManager.sortedEvents(events: filterManager.filteredEvents(events: events))
}.onReceive(filterManager.objectWillChange){
data = filterManager.sortedEvents(events: filterManager.filteredEvents(events: events))
}
I have a SwiftData @Query which gives me a array of Event objects. Due to the nature of the object (e.g. dynamic distance calculations) I filter the events manually using function.
This works fine for any changes in the objects, and they are shown directly in the SwiftUI List. But If I add an object the List is not updated (as it is created from the filtered list, not directly from the @Quary).
Is there a way to subscribe to the @Quary to get a notification (or to pass the state on) so the data is refreshed? This is the relevant code, the data variable contains the data sorted by day and filtered using a normal function. FilterManager is an observable object, which notifies if the filter parameters change.
@State var data: [Date:[Event]] = [:]
@Query(sort: \Event.startTime) private var events: [Event]
List {
ForEach(Array(data.keys).sorted(by: <), id: \.self) { day in
Section(header: Text(day,format: Date.FormatStyle()
.year(.defaultDigits)
.month(.abbreviated)
.day(.twoDigits)
.weekday(.abbreviated)).font(.footnote)) {
ForEach(data[day]!) { item in
[...]
}
}
}.onAppear(){
data = filterManager.sortedEvents(events: filterManager.filteredEvents(events: events))
}.onReceive(filterManager.objectWillChange){
data = filterManager.sortedEvents(events: filterManager.filteredEvents(events: events))
}
Share
Improve this question
asked Nov 20, 2024 at 22:16
Luuk D. JansenLuuk D. Jansen
4,5088 gold badges49 silver badges95 bronze badges
6
|
Show 1 more comment
2 Answers
Reset to default 1Use a computed property (that depends on events
), such as
var data: [Date:[Event]] {
filterManager.sortedEvents(events: filterManager.filteredEvents(events: events))
}
instead of
@State var data: [Date:[Event]] = [:]
Assuming you add Event
objects which trigger a refresh of the Query, you should be able to observe it:
.onChange(of: events.count) {
data = filterManager.sortedEvents(events: filterManager.filteredEvents(events: events))
}
本文标签: swiftuiNotifying a change in SwiftData queryStack Overflow
版权声明:本文标题:swiftui - Notifying a change in SwiftData @query - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742325246a2453572.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
var data: [Date:[Event]] { filterManager.sortedEvents(events: filterManager.filteredEvents(events: events)) }
instead of@State var data...
– workingdog support Ukraine Commented Nov 20, 2024 at 23:10@Query
property will be called a lot so this could lead to performance issues depending on what is being done in those sort and filter functions and of course on how much data you have. – Joakim Danielson Commented Nov 21, 2024 at 10:18id: \.self
will probably cause a crash since its not a valid keypath to unique identifier. AlsoonReceive
isn't right, SwiftUI already monitors observable objects automatically. – malhal Commented Nov 21, 2024 at 14:58