Data not passing correctly - SwiftUI, CoreData

Hi,

I am making a list app with SwiftUI. In the view which shows the list's items, each item has an info button which allows the user to edit the item. However, when I pass the item to that view, it does not pass the right item.

For example, I have a list of items:

Item 5 [Info Button]
Item 4 [Info Button]
Item 3 [Info Button]
Item 2. [Info Button]
Item 1. [Info Button]

If I tap on the Info Button for any one of the items, it will only show Item 5 in the Detail Edit View.

Here's the code:

Code Block swift
struct ItemsView: View {
    @Environment(\.managedObjectContext) private var viewContext
        
    @ObservedObject var group: ItemGroup
    @State private var showingDetailView = false
       var body: some View {
        ScrollView {
            VStack(alignment: .leading) {
                ForEach(group.ongoingItems) { item in
                    ItemRow(for: item)
                        .padding()
                }
    }
    func ItemRow(for item: Item) -> some View {
        HStack(spacing: 10) {
...
            Text(item.wrappedName)
                .fontWeight(.light)
            Spacer()
            Button(action: {
                showingDetailView.toggle()
            }, label: {
                Image(systemName: "info.circle").imageScale(.large)
            })
            .sheet(isPresented: $showingDetailView, content: {
                NavigationView {
                    ItemDetailView(selectedItem: item)
                }
            })
        }
    }


Code Block swift
struct ItemDetailView: View {
    @Environment(\.managedObjectContext) var viewContext
    @ObservedObject var selectedItem: Item
...



Answered by OOPer in 673946022
Having multiple sheets controlled by a single @State variable is not recommended.

Please try something like this:
Code Block
struct ItemsView: View {
@Environment(\.managedObjectContext) private var viewContext
@ObservedObject var group: ItemGroup
@State private var showingDetailView = false
@State var selectedItem: Item = Item()
var body: some View {
ScrollView {
VStack(alignment: .leading) {
ForEach(group.ongoingItems) { item in
itemRow(for: item)
.padding()
}
//...
}
//...
}
.sheet(isPresented: $showingDetailView, content: {
ItemInfoSheet(item: $selectedItem)
})
}
func itemRow(for item: Item) -> some View {
HStack(spacing: 10) {
//...
Text(item.wrappedName)
.fontWeight(.light)
Spacer()
Button(action: {
selectedItem = item
showingDetailView.toggle()
}, label: {
Image(systemName: "info.circle").imageScale(.large)
})
}
}
//...
}
struct ItemInfoSheet: View {
@Binding var item: Item
var body: some View {
NavigationView {
ItemDetailView(selectedItem: item)
}
}
}


Try changing line 12 by adding id:
Code Block
                ForEach(group.ongoingItems, id:\.self) { item in

@Claude31

Thanks for your reply. I added the 'id' but it still doesn't work.
So could you add complete code replicating the problem, so that we can test ?
Also try to change
Code Block
    @ObservedObject var group: ItemGroup

by
Code Block
    @State var group: ItemGroup

Accepted Answer
Having multiple sheets controlled by a single @State variable is not recommended.

Please try something like this:
Code Block
struct ItemsView: View {
@Environment(\.managedObjectContext) private var viewContext
@ObservedObject var group: ItemGroup
@State private var showingDetailView = false
@State var selectedItem: Item = Item()
var body: some View {
ScrollView {
VStack(alignment: .leading) {
ForEach(group.ongoingItems) { item in
itemRow(for: item)
.padding()
}
//...
}
//...
}
.sheet(isPresented: $showingDetailView, content: {
ItemInfoSheet(item: $selectedItem)
})
}
func itemRow(for item: Item) -> some View {
HStack(spacing: 10) {
//...
Text(item.wrappedName)
.fontWeight(.light)
Spacer()
Button(action: {
selectedItem = item
showingDetailView.toggle()
}, label: {
Image(systemName: "info.circle").imageScale(.large)
})
}
}
//...
}
struct ItemInfoSheet: View {
@Binding var item: Item
var body: some View {
NavigationView {
ItemDetailView(selectedItem: item)
}
}
}


Data not passing correctly - SwiftUI, CoreData
 
 
Q