@ObservedResults synced realm performance issues

I’ve been experiencing a bug when using @ObservedResults with a synced realm. The variable seems to be changing rapidly which invalidates the view many times per second.

I am passing a flexible synced realm to the view using view.environment(.realm, realm).

I’ve validated that the resulting ObservedResults variable is being updated rapidly by calling .onChange on it’s variable and monitoring how quickly that is being triggered.

I am using RealmSwift 10.25.1

Have anyone else seen this behavior with a synced realm?

Do you have a minimal amount of code available that replicates this issue? Can you post it?

@Jay Below is an abbreviated example that is similar to the structure of my app and should provide reproducibility.

You will notice 3 key indicators that the view is invalidating rapidly.

  1. The UI becomes extremely laggy
  2. OnChange of the realm var from @ObservedResults prints to console extremely quickly
  3. An error on the view onChange(of: Results<Dogs>) action tried to update multiple times per frame.

EDIT: I should note that I do not see the same invalidation issue if I replace the dogs variable in the onChange function with either the realm environmental variable passed to the view or the realm variable within the RealmModel if I pass the ObservableObject RealmModel to the HomeScreen view.

RealmModel.swift

import Foundation
import RealmSwift

public class RealmModel: ObservableObject {
  @Published var realm: Realm?
  @Published var loggedIn: Bool?
}

init() {
  app = RealmSwift.App(id: "_______")
  realmConfig = app?.currentUser?.flexibleSyncConfiguration()
  realmConfig?.objectTypes = [Dogs.self]
  realm = try! await Realm(configuration: realmConfig!, downloadBeforeOpen: .never)

  // Code to add subscriptions for Dogs
}

ContentView.swift

import SwiftUI
import RealmSwift

struct ContentView : View {
  @StateObject var realmModel = RealmModel()

    var body: some View {
      if realmModel.realm != nil{
        HomeScreenView()
          .environment(\.realm, auth.realm!)
        } 
      }
    }
}

HomeScreenView.swift

import SwiftUI
import RealmSwift

struct HomeScreenView: View {
  @ObservedResults(Dogs.self) var dogs

  var body: some View {
    ScrollView {
      ForEach(dogs) { dog in 
        Text("Dog object")
      }
    }
      .onChange(of: dogs) { newValue in
        print("View refreshing")
      }
  }
}

Thank you for taking a look

@Jay Did the above code help replicate this issue?