SwiftUI “List" Realm Guide

Can we please get a SwiftUI “List" Realm Guide ???

Someone! Anyone!

What would that be, specifically? Are you asking about a List Realm object? Or something else? Or are you asking about SwiftUI List?

Do you have some code to share you’re having difficulty with?

1 Like

I’m currently working on a project and have gotten as far as grabbing the Realm data results, placing them inside cells and deleting the data according to the cell row when swiping to delete said cell.

I’m having a fairly difficult time figuring out why I get the "Thread error : Out of Bounds”

& most importantly how to fix it…

Realm doesn’t have much/if any documentation/tutorials including Swift UI :frowning:

Here’s my code if you have any suggestions…

//Thanks in Advance!!!

import RealmSwift
 
 
let realm = try! Realm()
 
    class BindableResults<Element>: ObservableObject where Element: RealmSwift.RealmCollectionValue {
        var results: Results<Element>
        
        private var token: NotificationToken!
        
        init(results: Results<Element>) {
            self.results = results
            lateInit()
        }
        func lateInit() {
            token = results.observe { [weak self] _ in
                self?.objectWillChange.send()
            }
        }
        deinit {
            token.invalidate()
        }
    }

The Body:

@ObservedObject var diveLogs = BindableResults(results: try! Realm().objects(DiveLog.self).sorted(byKeyPath: “dateOfDive”))

var body: some View {
    
    VStack{
            
    List{
        ForEach(diveLogs.results, id: \.dateOfDive) { diveLog in
            DivePost(diveLog: diveLog)
        }.onDelete(perform: deleteRow )
    }

Delete Row Function:

private func deleteRow(with indexSet: IndexSet){
indexSet.forEach ({ index in
//Grabbing index of selected row for deletion
let mainIndex = index

        try! realm.write {
            realm.delete(self.diveLogs.results[mainIndex])
        }
    })
}

The deletion of the selected row/index works perfectly but I can’t quite figure out why I keep getting this error. Possibly to do with the data updating the List using Bindable Results func???

When running this code above I get this Error in the AppDelegate:

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
Thread 1: Exception: "Index 1 is out of bounds (must be less than 1).”

//The cell I selected to delete was the second item in the Array

If any additional information is needed, I will gladly provide…

Thanks in Advance!

@Kamron_Hopkins We are working on some developer blog posts right now for SwiftUI but perhaps this working example of SwiftUI with the latest RealmSwift can help you - realm-swift/examples/ios/swift/ListSwiftUI at master · realm/realm-swift · GitHub

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.