Hi all,
I know this is may not be the right forum to post questions about Realm but my issue has to do with the nature of how SwiftUI works.
Has anyone been able to successfully integrate
Realm
with SwiftUI
, especially deleting records/rows from a SwiftUI List
? I have tried a few different things but no matter what I do I get the same error. After reading some related threads I found out that other people have the same issue but I refuse to think that there is no current solution to integrate Realm with SwiftUI.The following code successfully presents all of the items from
Realm
in a SwiftUI List
, I can create new ones and they show up in the List
as expected, my issues is when I try to delete records from the List
by either manually pressing a button or by left-swiping to delete the selected row, I get an Index is out of bounds error.Here is my code.
Realm Model:
class Dog: Object {
@objc dynamic var name = ""
@objc dynamic var age = 0
@objc dynamic var createdAt = NSDate()
@objc dynamic var userID = UUID().uuidString
override static func primaryKey() -> String? {
return "userID"
}
}
SwiftUI Code:
class BindableResults: ObservableObject where Element: RealmSwift.RealmCollectionValue {
var results: Results
private var token: NotificationToken!
init(results: Results) {
self.results = results
lateInit()
}
func lateInit() {
token = results.observe { [weak self] _ in
self?.objectWillChange.send()
}
}
deinit {
token.invalidate()
}
}
struct DogRow: View {
var dog = Dog()
var body: some View {
HStack {
Text(dog.name)
Text("\(dog.age)")
}
}
}
struct ContentView : View {
@ObservedObject var dogs = BindableResults(results: try! Realm().objects(Dog.self))
var body: some View {
VStack{
List{
ForEach(dogs.results, id: \.name) { dog in
DogRow(dog: dog)
}.onDelete(perform: deleteRow )
}
Button(action: {
try! realm.write {
realm.delete(self.dogs.results[0])
}
}){
Text("Delete User")
}
}
}
private func deleteRow(with indexSet: IndexSet){
indexSet.forEach ({ index in
try! realm.write {
realm.delete(self.dogs.results[index])
}
})
}
}
ERROR
Terminating app due to uncaught exception ‘RLMException’, reason: ‘Index 23 is out of bounds (must be less than 23).’
Of course, the
23
changes depending on how many items are in the Realm
database, in this case, I had 24 records when I swiped and tapped the delete button.