ios – “Publishing changes from within view updates is not allowed” when searching Realm’s ObservedResults


With the snippet of code below, I get the following log (Fault) at each keystroke in the search field:

Publishing changes from within view updates is not allowed, this will cause undefined behavior.

import SwiftUI
import RealmSwift

class User: Object, Identifiable {
    @Persisted(primaryKey: true) var id: ObjectId
    @Persisted var name: String = ""
}

struct ContentView: View {
    @ObservedResults(User.self) var users
    @State private var filter = ""

    var body: some View {
        NavigationStack {
            List {
                ForEach(users) { user in
                    Text(user.name)
                }
            }
            .navigationTitle("Users")
            .searchable(text: $filter, collection: $users, keyPath: \.name)
        }
    }
}

Am I doing something wrong, or could this be a bug in RealmSwift?

Versions:

  • iOS 17.0
  • Xcode 15.0
  • RealmSwift 10.45.2

Latest articles

spot_imgspot_img

Related articles

Leave a reply

Please enter your comment!
Please enter your name here

spot_imgspot_img