title: Delete item from a section list on SwiftUI
body: <p><strong>Overview</strong></p>
  
  <p>im doing a simple app with core data I have two entity users and territory the app shows a list of the users in sections by territory the problem is In the delete action the list delete the user from the first section if I try to delete the second user from the second section it delete the second user from the first section. </p>
  
  <p>I think index set is getting wrong sending the index of the section but when I try to change the onDelete to my nested forEach don't work </p>
  
  <p>Here is the code</p>
  
  <pre><code>import SwiftUI
  
  struct ContentView: View {
      @Environment(\.managedObjectContext) var moc
      @FetchRequest(entity: User.entity(), sortDescriptors: [NSSortDescriptor(keyPath: \User.name, ascending: true)]) var users: FetchedResults&lt;User&gt;
      @FetchRequest(entity: Territory.entity(), sortDescriptors: [NSSortDescriptor(keyPath: \Territory.name, ascending: true)]) var territories: FetchedResults&lt;Territory&gt;
      @State private var showAddUser = false
  
  
         var body: some View {
             GeometryReader{ geometry in
                 NavigationView {
                     ZStack {
  
                         List {
                             ForEach(self.territories, id: \.self) { territorie in
                              Section(header: Text(territorie.wrappedName)) {
                                  ForEach(territorie.usersArray, id: \.self) { user in
                                      NavigationLink(destination: UserView(user: user)) {
                                          VStack{
                                              HStack{
                                                 Text("user")
                                                  Spacer()
                                                  Text(user.dayLastVisit)
                                                      .padding(.horizontal)
                                              }
                                              HStack {
                                                  Text(user.wrappedEmoji)
                                                      .font(.largeTitle)
                                                  VStack(alignment: .leading) {
                                                      Text("\(user.wrappedName + " " + user.wrappedLastName)")
                                                          .font(.headline)
                                                      Text(user.wrappedType)
  
                                                  }
                                                  Spacer()
                                              }
                                          }
  
                                      }
                                  }.onDelete(perform: self.deleteItem)
                              }
                             }
  
  
  
                         }
                         .listStyle(GroupedListStyle())
                         .environment(\.horizontalSizeClass, .regular)
  
                         VStack {
                             Button(action:{ self.showAddRUser.toggle()}){
                                 ButtonPlus(icon:"plus")}
                             .offset(x: (geometry.size.width * 0.40), y: (geometry.size.height  * 0.38))
                             .sheet(isPresented: self.$showAddUser){
                                 NewUserView().environment(\.managedObjectContext, self.moc)
                                    }
                         }
                     }
                 .navigationBarTitle("Users")
                     .navigationBarItems( trailing: HStack {
                      EditButton()
                      Button(action:{self.showAddUser.toggle()}){
                      ButtonNew(text:"Nueva")}
                      }
  
                      .sheet(isPresented: self.$showAddUser){
                          NewUserView().environment(\.managedObjectContext, self.moc)
                     }
                     )
  
                 }
             }
         }
      func deleteItem(at offsets: IndexSet) {
          for offset in offsets { 
              let user = users[offset]
  
              //borarlo del context
              moc.delete(user)
  
          }
          try? moc.save()
      }
  
  }
  
  struct ContentView_Previews: PreviewProvider {
      static var previews: some View {
          ContentView()
      }
  }
  
  </code></pre>
  
  <p>im learning swift and swiftui so im would appreciate any help</p>