I am building a SwiftUI application using SwiftData. I have a list of Note objects that I need to display in different subviews grouped by year, month, and day. However, I’m running into issues passing the list of notes to these subviews and ensuring they update correctly when the data changes.
<code>struct MainView: View {
@Environment(.modelContext) var modelContext
@Query private var notesList: [Note]
@State private var notes = [Note]()
var body: some View {
ScrollView(showsIndicators: false) {
if !notes.isEmpty{
DaySectionView(notes: $notes)
}else{
ContentUnavailableView{
Label("No Notes", systemImage: "note.text")
.font(.largeTitle.bold())
}
.frame(maxHeight: .infinity, alignment: .center)
}
}
.onChange(of: notesList, initial: true){
notes = notesList
}
}
}
</code>
<code>struct MainView: View {
@Environment(.modelContext) var modelContext
@Query private var notesList: [Note]
@State private var notes = [Note]()
var body: some View {
ScrollView(showsIndicators: false) {
if !notes.isEmpty{
DaySectionView(notes: $notes)
}else{
ContentUnavailableView{
Label("No Notes", systemImage: "note.text")
.font(.largeTitle.bold())
}
.frame(maxHeight: .infinity, alignment: .center)
}
}
.onChange(of: notesList, initial: true){
notes = notesList
}
}
}
</code>
struct MainView: View {
@Environment(.modelContext) var modelContext
@Query private var notesList: [Note]
@State private var notes = [Note]()
var body: some View {
ScrollView(showsIndicators: false) {
if !notes.isEmpty{
DaySectionView(notes: $notes)
}else{
ContentUnavailableView{
Label("No Notes", systemImage: "note.text")
.font(.largeTitle.bold())
}
.frame(maxHeight: .infinity, alignment: .center)
}
}
.onChange(of: notesList, initial: true){
notes = notesList
}
}
}
<code>struct DaySectionView: View {
@Environment(.modelContext) private var modelContext
@Binding var notes: [Note]
@State private var remainingNotes = [Note]()
var body: some View {
let firstNoteDay = Calendar.current.dateComponents([.day], from: notes.first!.date).day ?? 0
let notesForSameDay = notes.filter {
Calendar.current.dateComponents([.day], from: $0.date).day == firstNoteDay
}
ForEach(notesForSameDay, id: .self){ note in
NavigationLink{
NoteDetailsView(note: .constant(note))
}label: {
NoteCardView(note: note)
}
}
.onChange(of: notes, initial: true){
guard let firstNote = notes.first else { return }
remainingNotes = notes.filter {
Calendar.current.dateComponents([.day], from: $0.date).day != firstNoteDay
}
}
if !remainingNotes.isEmpty {
DaySectionView(notes: $remainingNotes)
}
}
}
</code>
<code>struct DaySectionView: View {
@Environment(.modelContext) private var modelContext
@Binding var notes: [Note]
@State private var remainingNotes = [Note]()
var body: some View {
let firstNoteDay = Calendar.current.dateComponents([.day], from: notes.first!.date).day ?? 0
let notesForSameDay = notes.filter {
Calendar.current.dateComponents([.day], from: $0.date).day == firstNoteDay
}
ForEach(notesForSameDay, id: .self){ note in
NavigationLink{
NoteDetailsView(note: .constant(note))
}label: {
NoteCardView(note: note)
}
}
.onChange(of: notes, initial: true){
guard let firstNote = notes.first else { return }
remainingNotes = notes.filter {
Calendar.current.dateComponents([.day], from: $0.date).day != firstNoteDay
}
}
if !remainingNotes.isEmpty {
DaySectionView(notes: $remainingNotes)
}
}
}
</code>
struct DaySectionView: View {
@Environment(.modelContext) private var modelContext
@Binding var notes: [Note]
@State private var remainingNotes = [Note]()
var body: some View {
let firstNoteDay = Calendar.current.dateComponents([.day], from: notes.first!.date).day ?? 0
let notesForSameDay = notes.filter {
Calendar.current.dateComponents([.day], from: $0.date).day == firstNoteDay
}
ForEach(notesForSameDay, id: .self){ note in
NavigationLink{
NoteDetailsView(note: .constant(note))
}label: {
NoteCardView(note: note)
}
}
.onChange(of: notes, initial: true){
guard let firstNote = notes.first else { return }
remainingNotes = notes.filter {
Calendar.current.dateComponents([.day], from: $0.date).day != firstNoteDay
}
}
if !remainingNotes.isEmpty {
DaySectionView(notes: $remainingNotes)
}
}
}
<code>// NoteDetailsView
@Binding var note: Note
// NoteCardView
var note: Note
</code>
<code>// NoteDetailsView
@Binding var note: Note
// NoteCardView
var note: Note
</code>
// NoteDetailsView
@Binding var note: Note
// NoteCardView
var note: Note
I tried to use @Bindable, but doesn’t works with me.
All models working very well.
New contributor
faisal almalki is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.