Tim
Tim

Reputation: 129

SwiftUI - Pass a fetchResult to another View

I'm trying to pass a FetchResult to another view in order to have all my tables updated at the same time.

My problem:

view1 {
@FetchRequest
ForEach{
NavigationLink(passing fetchRequest.value to View 2)}
}

View2 {
var value1 :fetchRequest.value from view 1
ForEach{
NavigationLink(passing value1.value to View 3)}
}

View3....

Problem here is, if I do a delete or a add on the view 3, the views 1 and 2 won't update until I go back to view 1, and descend again to view 2 and 3.

Do you have an idea on how to have a quick update of these values ?

Best

Tim

Upvotes: 3

Views: 2523

Answers (1)

malhal
malhal

Reputation: 30549

I've never seen anyone else try this before but I just lifted the @FetchRequest up into a superview and passed the fetch results (items in this case) as a param down to the subview:

struct ContentView: View {
    @State var count = 0
    @FetchRequest<Item>(sortDescriptors: [], predicate: nil, animation: nil) var items
    
    var body: some View {
        NavigationView {
            MasterView(items: items)
                .navigationTitle("Master \(count)")
                .navigationBarItems(trailing: Button("Increment"){
                    count += 1
                })
        }
    }
}


struct MasterView: View {
    var items : FetchedResults<Item>
    
    var body: some View {
        List {
            ForEach(items) { item in
                   Text("Item at \(item.timestamp!, formatter: itemFormatter)")
                }
                .onDelete(perform: deleteItems)
            }
        .toolbar {
//            #if os(iOS)
//            ToolbarItem(placement: .navigation){
//                EditButton()
//            }
//            #endif
            //ToolbarItem(placement: .automatic){
            ToolbarItem(placement: .bottomBar){
                Button(action: addItem) {
                    Label("Add Item", systemImage: "plus")
                }
            }
            ToolbarItem(placement: .bottomBar){
                Button(action: {
                    ascending.toggle()
                }) {
                    Text(ascending ? "Descending" : "Ascending")
                }
            }
        }
    }
    
    
    private func addItem() {
        
        withAnimation {
            let newItem = Item(context: viewContext)
            newItem.timestamp = Date()
            newItem.name = "Master"
            
            do {
                try newItem.validateForInsert()
                try viewContext.obtainPermanentIDs(for: [newItem])
                try viewContext.save()
            } catch {
                // Replace this implementation with code to handle the error appropriately.
                // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
                let nsError = error as NSError
                fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
            }
        }
    }

    private func deleteItems(offsets: IndexSet) {
        withAnimation {
            offsets.map {items[$0] }.forEach(viewContext.delete)

            do {
                try viewContext.save()
            } catch {
                // Replace this implementation with code to handle the error appropriately.
                // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
                let nsError = error as NSError
                fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
            }
        }
    }
}

The reason I did this is I used a the launch argument -com.apple.CoreData.SQLDebug 4 and I noticed it was hitting the database every time the state changed and a View was recreated that contained a @FetchRequest which I didn't want.

Upvotes: 3

Related Questions