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

Got exception when update subscription in an update block

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

      How frequently does the bug occur?

      Always

      Description

      I try to follow the document to update my exist subscription, but it crash when the subscription already exist.

      Stacktrace & log output

      Unable to find source-code formatter for language: shell. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
      Cart[70214:14267747] *** Terminating app due to uncaught exception 'RLMException', reason: 'Can only add, remove, or update subscriptions within a write subscription block.'
      *** First throw call stack:
      (
      	0   CoreFoundation                      0x000000018040c304 __exceptionPreprocess + 172
      	1   libobjc.A.dylib                     0x000000018005131c objc_exception_throw + 56
      	2   Realm                               0x000000010f7bb3a4 -[RLMSyncSubscriptionSet verifyInWriteTransaction] + 72
      	3   Realm                               0x000000010f7ba31c -[RLMSyncSubscriptionSet addSubscriptionWithClassName:subscriptionName:predicate:updateExisting:] + 112
      	4   Realm                               0x000000010f7b8148 -[RLMSyncSubscription updateSubscriptionWithPredicate:] + 216
      	5   RealmSwift                          0x000000010bf9e6fc $s10RealmSwift16SyncSubscriptionV11updateQuery6toType5whereyxm_AA0F0VySbGAHyxGcSgtSo0aB6ObjectCRbzlF + 780
      ...
      	7   RealmSwift                          0x000000010bf9f718 $sIg_Ieg_TR + 20
      	8   RealmSwift                          0x000000010bf9f770 $sIeg_IyB_TR + 24
      	9   Realm                               0x000000010f7b9030 -[RLMSyncSubscriptionSet update:onComplete:] + 264
      	10  RealmSwift                          0x000000010bfa1a50 $s10RealmSwift19SyncSubscriptionSetV6updateyyyyXEYaKFTY0_ + 376
      ...
      	14  libswift_Concurrency.dylib          0x00000001ae384d0d _ZL23completeTaskWithClosurePN5swift12AsyncContextEPNS_10SwiftErrorE + 1
      )
      

      Can you reproduce the bug?

      Always

      Reproduction Steps

      The code just like doc:

      let realm = try await checkAndAddSubscription()
      
      // Opening a realm and accessing it must be done from the same thread.
      // Marking this function as `@MainActor` avoids threading-related issues.
      @MainActor
      func checkAndAddSubscription() async throws -> Realm {
          let realm = try await Realm(configuration: flexSyncConfig)
          let subscriptions = realm.subscriptions
          let foundSubscription = subscriptions.first(named: "user_team")
          try await subscriptions.update {
              if foundSubscription != nil {
                  foundSubscription!.updateQuery(toType: Team.self, where: {
                       $0.teamName == "Developer Education"   // CRASH_HERE
                  })
              } else {
                  subscriptions.append(
                      QuerySubscription<Team>(name: "user_team") {
                        $0.teamName == "Developer Education"
                     })
              }
          }
          return realm
      }
      
      

      The only difference is Team was my personal Realm object.

      Version

      10.41.0

      What Atlas Services are you using?

      Both Atlas Device Sync and Atlas App Services

      Are you using encryption?

      No

      Platform OS and version(s)

      iOS 16

      Build environment

      Xcode version: 14.3.1

            Assignee:
            lee.maguire@mongodb.com Lee Maguire
            Reporter:
            unitosyncbot Unito Sync Bot
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: