I´ve got a list of Fetched Core Data Items, displayed as a NavigationLink inside a ForEach Loop. Each of those Elements can be deleted by Swipe or Context Menu. However when I add an additional confirmationDialog, and move the actual delete action into that one, the wrong item gets deleted (until the actual selected Item is the last one). Without the confirmationDialog, and the delete Action inside the Button, it works fine. Does anyone have any idea why?
Thank you!
import Foundation
import SwiftUI
struct IngredientsList: View {
@Environment(\.managedObjectContext) private var viewContext
@FetchRequest(sortDescriptors: []) private var ingredients: FetchedResults<Ingredient>
@State private var DeleteDialogue = false
var body: some View {
VStack{
List{
ForEach(ingredients){ingredients in
NavigationLink{
RecipeIngredientsDetailed(ingredients: ingredients, editMode: true, createmode: true)
} label: {
Text(ingredients.ingredientname ?? "")
}
.swipeActions(){
Button(role: .destructive){
DeleteDialogue = true
} label:{
Text("Delete")
}
}
.contextMenu(){
Button(role: .destructive){
DeleteDialogue = true
} label:{
Text("Delete")
}
}
.confirmationDialog("Are you sure?", isPresented: $DeleteDialogue){
Button("Delete Ingredient"){
viewContext.delete(ingredients)
do{
try viewContext.save()
} catch{
}
}
} message: {
Text("This will remove the Ingredient from all Recipes!")
}
}
}
}
}
}