SwiftUI Empty List Auto Refresh

Viewed 179

I have a list of data from MongoDB Realm which is being displayed as a list on my View, however I want to show the Text("No list available") when the Realm has no data in it. I implemented the same with an if - else loop, however when a data is added, the list still shows "No list available", and doesn't listen for the changes immediately, I have to restart the app for the list to be seen. How do I get my view to listen for a change and then display either the list with values or "No list available" then itself?

This is my code:

@State var data = try! Realm().objects(Diary.self).filter("userID = '\(uid!)'")

if data.isEmpty {
     Text("No Data")
} else {
 List{
    ForEach(data) { value in
      Section(header: Text(value.date!)){
        VStack {
          HStack {
           Text(value.title!)
           Spacer()
           }
        .padding(.bottom, 5)
         HStack {
          Text(value.mood!)
          Spacer()
              }
           }
        }
     }
   }
}
0 Answers
Related