Uploaded image for project: 'Realm Cocoa SDK'
  1. Realm Cocoa SDK
  2. RCOCOA-1435

RealmSwift: @ObservedResults not updating with Realm Sync

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: None

      How frequently does the bug occur?

      All the time

      Description

      I’m trying to use Realm Sync for a basic CRUD app using SwiftUI, but for some reason the @ObservedResults property wrapper is not updating my view when I add/delete objects.

      The Realm App is set up on the free shared tier. When I insert/delete entries, I can see in the Realm App logs that the data is being inserted/deleted appropriately in the collection. The problem is that the SwiftUI View (using the @ObservedResults wrapper) does not update appropriately. On the other hand, if I quit and restart the iOS app, any insertions/deletions I’ve made DO show up.

      I brought this up on the forums and was asked to file an issue here.

      Stacktrace & log output

      No response

      Can you reproduce the bug?

      Yes, always

      Reproduction Steps

      Relevant portions of code:

      let app = RealmSwift.App(id: "[XXXXXXXXX]")
      
      class AppState: ObservableObject {
          @Published var userID: String?
      }
      
      @main
      struct RepositoryRealm_App: SwiftUI.App {
          @StateObject var appState = AppState()
          var body: some Scene {
              WindowGroup {
                  switch appState.userID {
                  case nil:
                      Text("XXXX")
                          .onAppear {
                              switch app.currentUser {
                              case nil:
                                  app.login(credentials: .anonymous) { result in
                                      DispatchQueue.main.async {
                                          switch result {
                                          case .failure(let error):
                                              fatalError(error.localizedDescription)
                                          case .success(let user):
                                              let partitionValue = user.id
                                              appState.userID = partitionValue
                                          }
                                      }
                                  }
                                  
                              case let currentUser?:
                                  let partitionValue = currentUser.id
                                  appState.userID = partitionValue
                              }
                          }
                      
                  case let userID:
                      ContentView()
                          .environmentObject(appState)
                          .environment(\.partitionValue, userID)
                  }
              }
          }
      }
      
      struct ContentView: View {
          @AsyncOpen(appId: "[XXXXXXXXX]", partitionValue: "", timeout: 5000) var asyncOpen
          var body: some View {
              switch asyncOpen {
              case .connecting:
                  Text("Connecting...")
              case .waitingForUser:
                  Text("Waiting for user...")
              case .open(let userRealm):
                  EntryObjectListView()
                      .environment(\.realm, userRealm)
              case .progress(let progress):
                  ProgressView(progress)
              case .error(let error):
                  fatalError(error.localizedDescription)
              }
          }
      }
      
      struct EntryObjectListView: View {
          @EnvironmentObject var appState: AppState
          @ObservedResults(EntryObject.self) var entryObjects
          
          var body: some View {
              NavigationView {
                  VStack {
      
                      List {
                          ForEach(entryObjects) { entryObject in
                              Text(entryObject.title)
                          }
                          .onDelete(perform: $entryObjects.remove)
                      }
                      Spacer()
                      Button("add text", systemImage: "plus") {
      
                          let entryObject = EntryObject(dataType: TextData.self)
                          entryObject.title = "hello text!"
                          entryObject._partition = appState.userID!
      
                          $entryObjects.append(entryObject)
                      }
                      Button("delete all", systemImage: "minus") {
                          withAnimation {
                              for entryObject in entryObjects {
                                  $entryObjects.remove(entryObject)
                              }
                          }
                      }
                      Button("print count", systemImage: "minus") {
                          print(entryObjects.count)
                      }
                  }
              }
          }
      }
      
      final class EntryObject: Object, ObjectKeyIdentifiable {
          
          @Persisted(primaryKey: true) var _id = UUID()
          @Persisted var _partition: String = ""
          @Persisted var type: EntryType = .undefined
          @Persisted var dateCreated: Date = Date()
          @Persisted var dateModified: Date = Date()
          @Persisted var title: String
          
          @Persisted var textData: TextData?
          @Persisted var urlData: URLData?
          @Persisted var objectData: ObjectData?
      
      // Some convenience methods / inits omitted
      }
      

      Version

      Realm 10.15.1

      What SDK flavour are you using?

      MongoDB Realm (i.e. Sync, auth, functions)

      Are you using encryption?

      No, not using encryption

      Platform OS and version(s)

      iOS 14.7.1

      Build environment

      Xcode version: 13.0

            Assignee:
            jason.flax@mongodb.com Jason Flax
            Reporter:
            unitosyncbot Unito Sync Bot
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: