0

I have a list of items, each of which has a navigation link. When triggered, it calls a UIViewController from SwiftUI, some data is manipulated and then it returns. The pdfsList array needs to be re-fetched when I return from the view controller. How do I make that happen?

struct ContentView : View {
    @State var pdfsList: [PDFSummary] = Realm.studyHallRealm.objects(PDFSummary.self).sorted(by: { $0.name < $1.name })

    var body: some View {
      NavigationView {
        List(pdfsList) { pdfSummary in
            NavigationLink(destination: InterfaceController(url: pdfSummary.dynamicURL(), summary: pdfSummary)) {
                Text(pdfSummary.name)
                .foregroundColor(self.itemColor(pdfSummary: pdfSummary))
            }
        }.navigationBarTitle(Text("PDF Library (SwiftUI)"))
    }
}  

2 Answers 2

1

I think you can fetch your data on .onAppear action on your list

NavigationView {
        List(pdfsList) { pdfSummary in
            NavigationLink(destination: InterfaceController(url: pdfSummary.dynamicURL(), summary: pdfSummary)) {
                Text(pdfSummary.name)
                .foregroundColor(self.itemColor(pdfSummary: pdfSummary))
            }
        }.navigationBarTitle(Text("PDF Library (SwiftUI)"))
         .onAppear {
               self.pdfsList = Realm.studyHallRealm.objects(PDFSummary.self).sorted(by: { $0.name < $1.name })
            }
    }
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. I’ll try that later when I get home. It sounds right. 🙏🏻
It didn't work. .onAppear is called, but the display is not updated.
0

Solved --Thanks to Mac3n -- His answer definitely deserves an upvote!

After adding his suggestion to the mix, it still wasn't working, though.

  • I created a shared singleton instance of my data model.
  • I added a refresh() method to call anytime any of the data is updated.
  • I used @Published on the model data array and
  • I made the DataModel an ObservableObject.

     class DataModel: ObservableObject {
        static let sharedInstance = DataModel()
        @Published var modelData: [PDFSummary]
    
        init() {
           self.modelData = Realm.studyHallRealm.objects(PDFSummary.self).sorted(by: { $0.name < $1.name })
        }
    
        func refresh() {
           modelData = Realm.studyHallRealm.objects(PDFSummary.self).sorted(by: { $0.name < $1.name })
        }
     }
    

In the main view the "shared" modelData is declared as an ObservedObject.

Every time the view appears, refresh() is called in case the data was updated.

struct ContentView : View {
    /// the datasource, a Realm db of PDFSummary objects
    @ObservedObject var modelData = DataModel.sharedInstance

    var body: some View {
            List(self.modelData.modelData) { pdfSummary in
                NavigationLink(destination: InterfaceController(url: pdfSummary.dynamicURL()!, summary: pdfSummary)) {
                    Text(pdfSummary.name)
                        .foregroundColor(self.itemColor(pdfSummary: pdfSummary))
                }
            }.navigationBarTitle(Text("PDF Library (SwiftUI)"))
            .onAppear {
                self.modelData.refresh()
            }
    }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.