title: How to delete data from SwiftUI List and Realm
body: <p>The following code properly displays all of the 'Users' from <code>Realm</code> database in a SwiftUI <code>List</code>. My issue is deleting records when I swipe a row.</p>
  
  <p>When I swipe a row and tap the delete button, I immediately get an <code>uncaught exception</code> error, the <code>List</code> does not update but I know the right item gets deleted from the <code>Realm</code> database since the next time I run the app the selected record doesn't show up.</p>
  
  <p>Here is my code.</p>
  
  <h2>SwiftUI Code</h2>
  
  <pre><code>import RealmSwift
  
  struct ContentView: View {
  
      @State private var allUsers: Results&lt;User&gt; = realm.objects(User.self)
  
      var body: some View {
          VStack{
              Text("Second Tab")
              List{
                  ForEach(allUsers, id:\.self) { user in
                      HStack{
                          Text(user.name)
                          Text("\(user.age)")
                      }
                  }.onDelete(perform: deleteRow)
              }
          }
      }
  
      private func deleteRow(with indexSet: IndexSet){
          indexSet.forEach ({ index in
              try! realm.write {
                  realm.delete(self.allUsers[index])
              }
          })
      }
  }
  </code></pre>
  
  <h2>Realm Model</h2>
  
  <pre><code>import RealmSwift
  
  class User:Object{
      @objc dynamic var name:String = ""
      @objc dynamic var age:Int = 0
      @objc dynamic var createdAt = NSDate()
  
      @objc dynamic var userID = UUID().uuidString
      override static func primaryKey() -&gt; String? {
          return "userID"
      }
  }
  </code></pre>
  
  <h2>ERROR</h2>
  
  <blockquote>
    <p>Terminating app due to uncaught exception 'RLMException', reason: 'Index 4 is out of bounds (must be less than 4).'</p>
  </blockquote>
  
  <p>Of course, the <code>4</code> changes depending on how many items are in the <code>Realm</code> database, in this case, I had 5 records when I swiped and tapped the delete button.</p>
  
  <p>My expectation was that the <code>List</code> was going to update every time the <code>allUsers</code> @State variable changes, I know my issue is not fully understanding how binding works.</p>
  
  <p>What am I doing wrong?</p>