Refresh value in view SwiftUI

Hello

I am making a counter app using Core Data to store the value that the user has reached, the problem is that in the DetailView the value changes just if I close the view and reopen it, how can I change the value immediately when the user taps the button?

ContentView:

struct ContentView: View {
    @Environment(\.managedObjectContext) private var viewContext
    
    @FetchRequest(
        sortDescriptors: [NSSortDescriptor(keyPath: \Item.date, ascending: true)],
        animation: .default)
    private var items: FetchedResults<Item>
    
    @State private var isShown: Bool = false
    var body: some View {
        NavigationView{
            List {
                Section(header: Text("All counters")){
                    ForEach(items) { item in
                        NavigationLink(
                            destination:
                                DetailView(item: item)
                                .environment(\.managedObjectContext, viewContext)
                            ,
                            label: {
                                HStack{
                                    Text(item.name ?? "")
                                        .font(.title3)
                                        .fontWeight(.semibold)
                                    
                                    Spacer()
                                    
                                    
                                    Text("\(item.value, specifier: "%.0f")")
                                        .font(.title3)
                                        .fontWeight(.semibold)
                                }
                            })
                    }
                    .onDelete(perform: { indexSet in
                        deleteItems(offsets: indexSet)
                        print(items)
                    })
                }
            }
            .listStyle(InsetGroupedListStyle())
            .sheet(isPresented: $isShown, content: {
                AddView()
                    .environment(\.managedObjectContext, viewContext)
            })
            .navigationBarTitle("Counter")
            .toolbar {
                Menu {
                    Button(action: {
                        isShown.toggle()
                    }) {
                        Text("Add Item")
                    }
                    
                    EditButton()
                } label: {
                    Image(systemName: "plus")
                        .font(.title)
                }
            }
        }
    }
    
    private func deleteItems(offsets: IndexSet) {
        withAnimation {
            offsets.map { items[$0] }.forEach(viewContext.delete)
            
            do {
                try viewContext.save()
            } catch {
                print(error.localizedDescription)
            }
        }
    }
}

DetailView:

struct DetailView: View {
    
    @Environment(\.managedObjectContext) private var viewContext
    
    var item: Item
    
    var body: some View {
        VStack(alignment: .center, spacing: nil, content: {
            Text("\(item.value, specifier: "%.0f")")
                .font(.largeTitle)
                .fontWeight(.bold)
                .padding(.top)
            
            Spacer()
            
            HStack{
                Button(action: {
                    withAnimation{
                        item.value += 1
                        
                        do {
                            try viewContext.save()
                        } catch {
                            print(error.localizedDescription)
                        }
                    }

                }, label: {
                    Image(systemName: "plus")
                })
                
                
                Button(action: {
                    withAnimation{
                        item.value -= 1
                        
                        do {
                            try viewContext.save()
                        } catch {
                            print(error.localizedDescription)
                        }
                    }
                }, label: {
                    Image(systemName: "minus")
                })
            }
            .foregroundColor(.primary)
            
            Spacer()
        })
        .navigationBarTitle(item.name ?? "", displayMode: .inline)
    }
}

Thank you

Answered by DelawareMathGuy in 679792022

hi,

change the declaration var item: Item in DetailView to

@ObservedObject var item: Item

changes to the item will then trigger a view update for the DetailView. that should do it, i think.

hope that helps,

DMG

Accepted Answer

hi,

change the declaration var item: Item in DetailView to

@ObservedObject var item: Item

changes to the item will then trigger a view update for the DetailView. that should do it, i think.

hope that helps,

DMG

Refresh value in view SwiftUI
 
 
Q