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

Crash when retrieving object: `No object with key '1398' in 'class_XXX'

      How frequently does the bug occur?

      Sometimes

      Description

      I'm getting fatal crashes when using the realm/object(ofType:forPrimaryKey method. Below is the wrapper method I use to retrieve objects. You'll notice from the log output that the actual key (the id used) is a String, not an integer 1398 as reported by the crash.

      extension Realm {
          public func get<O: Object & Identifiable>(_ object: O.Type, id: O.ID) throws -> O
          {
              let ticket = UUID().uuidString
              debugLog("[Realm] [\(ticket)] Retrieving [\(String(describing: O.self))] with id [\(id)]")
              
              guard let result = self.object(ofType: O.self, forPrimaryKey: id) else { // <-- crash here
                  throw Error.objectNotFoundError
              }
              
              debugLog("[Realm] [\(ticket)] Finished")
              return result
          }
      
          // Note that objects' `id` properties (from the `Identifiable` conformance) are also 
          // the primary keys from a Realm Object perspective. (Otherwise this wouldn't work).
      }
      

      Apart from the fact that the keys don't seem to match up; why does this method cause a fatal crash? Why not just make it a throwing method, or return nil when the object can't be found? Why does this crash message exist at all?

      Also probably important to mention is that these issues have been occurring since transitioning to Actor-isolated Realms.

      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
      "[Realm] [6EE3ED4E-FB4F-4881-96D0-91307206C28D] Retrieving [TypeField] with id [AE96A70B-8E7D-48A8-9143-815B4035DFDB]"
      2023-06-09 16:42:52.186002+0200 DevApp[12906:3651096] *** Terminating app due to uncaught exception 'RLMException', reason: 'No object with key '1398' in 'class_TypeField''
      *** First throw call stack:
      (
      	0   CoreFoundation                      0x0000000180437330 __exceptionPreprocess + 172
      	1   libobjc.A.dylib                     0x0000000180051274 objc_exception_throw + 56
      	2   DevApp                          0x000000010345dc98 RLMGetObject + 796
      	3   DevApp                          0x0000000103726c00 $s10RealmSwift0A0V6object6ofType13forPrimaryKeyxSgxm_q_tSo0aB6ObjectCRbzr0_lF + 456
      	4   DevApp                          0x00000001043abc08 $s10RealmSwift0A0V7DevE3get_2idxxm_2IDQztKSo0aB6ObjectCRbzs12IdentifiableRzlF + 1140
      	5   DevApp                          0x00000001043afe68 $s7Dev0A6ObjectPAAE3get2id12forceRefreshxSg2IDQz_SbtFZ + 416
      	6   DevApp                          0x000000010431c1f0 $s7Dev16TabRowDefinitionV4CellV8cellTypeAE0eG0Ovg + 228
      	7   DevApp                          0x0000000104383f70 $s7Dev25InspectionPointPropertiesV14SectionBuilderV11processCell33_2513D25A16620545D96A1A40AF01868ALLyyAA16TabRowDefinitionV0H0VF + 100
      	8   DevApp                          0x0000000104383ef8 $s7Dev25InspectionPointPropertiesV14SectionBuilderV10processRow33_2513D25A16620545D96A1A40AF01868ALLyyAA03TabH10DefinitionVFyAI4CellVXEfU_ + 72
      	9   DevApp                          0x0000000104387d9c $s7Dev25InspectionPointPropertiesV14SectionBuilderV10processRow33_2513D25A16620545D96A1A40AF01868ALLyyAA03TabH10DefinitionVFyAI4CellVXEfU_TA + 24
      	10  libswiftCore.dylib                  0x000000018bd57490 $sSTsE7forEachyyy7ElementQzKXEKF + 440
      	11  DevApp                          0x00000001043824ac $s7Dev25InspectionPointPropertiesV14SectionBuilderV10processRow33_2513D25A16620545D96A1A40AF01868ALLyyAA03TabH10DefinitionVF + 332
      	12  DevApp                          0x0000000104382328 $s7Dev25InspectionPointPropertiesV14SectionBuilderV5build8fromRowsSayAC0E0OGSayAA16TabRowDefinitionVG_tFyALXEfU_ + 80
      	13  DevApp                          0x0000000104382354 $s7Dev25InspectionPointPropertiesV14SectionBuilderV5build8fromRowsSayAC0E0OGSayAA16TabRowDefinitionVG_tFyALXEfU_TA + 24
      	14  libswiftCore.dylib                  0x000000018bd57490 $sSTsE7forEachyyy7ElementQzKXEKF + 440
      	15  DevApp                          0x000000010437c7f0 $s7Dev25InspectionPointPropertiesV14SectionBuilderV5build8fromRowsSayAC0E0OGSayAA16TabRowDefinitionVG_tF + 120
      	16  DevApp                          0x000000010437c398 $s7Dev25InspectionPointPropertiesV3tab21generalPopertySectionAcA13TabDefinitionC_AA19GeneralPropertyFormVSgtcfC + 288
      	17  DevApp                          0x00000001042e3514 $s7Dev19InspectionPointTypeC15getPropertyTabs10forServiceSayAA0bC10PropertiesVGAA0I0C_tFAgA13TabDefinitionCXEfU_ + 368
      	18  DevApp                          0x00000001042e3568 $s7Dev19InspectionPointTypeC15getPropertyTabs10forServiceSayAA0bC10PropertiesVGAA0I0C_tFAgA13TabDefinitionCXEfU_TA + 28
      	19  libswiftCore.dylib                  0x000000018bcc120c $sSlsE3mapySayqd__Gqd__7ElementQzKXEKlF + 400
      	20  DevApp                          0x00000001042e32a8 $s7Dev19InspectionPointTypeC15getPropertyTabs10forServiceSayAA0bC10PropertiesVGAA0I0C_tF + 176
      	21  DevApp                          0x00000001041be17c $s7Dev14InspectionDataV3ups9isPreviewAcA18UserProjectServiceC_SbtcfcySDySiSayAA0B15PointPropertiesVGGz_AA0bJ4TypeCtXEfU1_ + 132
      	22  DevApp                          0x00000001041be590 $s7Dev14InspectionDataV3ups9isPreviewAcA18UserProjectServiceC_SbtcfcySDySiSayAA0B15PointPropertiesVGGz_AA0bJ4TypeCtXEfU1_TA + 24
      	23  libswiftCore.dylib                  0x000000018bd3289c $sSTsE6reduce4into_qd__qd__n_yqd__z_7ElementQztKXEtKlF + 480
      	24  DevApp                          0x00000001041bd900 $s7Dev14InspectionDataV3ups9isPreviewAcA18UserProjectServiceC_SbtcfC + 1768
      	25  DevApp                          0x000000010316cc88 $s10DevApp19InspectionViewModelC8loadData3ups9isPreviewy0A018UserProjectServiceC_SbtF + 212
      	26  DevApp                          0x0000000103302320 $s10DevApp16InspectionEditorV8loadData33_D6E6971756F86962CEE282E88253F5D3LLyyYaYbFTY0_ + 968
      	27  DevApp                          0x0000000103301df1 $s10DevApp16InspectionEditorV4bodyQrvgyyYaYbScMYccACcfu0_yyYaYbScMYccfu1_TQ0_ + 1
      	28  DevApp                          0x0000000103306dbd $s10DevApp16InspectionEditorV4bodyQrvgyyYaYbScMYccACcfu0_yyYaYbScMYccfu1_TATQ0_ + 1
      	29  SwiftUI                             0x000000010f06f74d objectdestroy.330Tm + 2381
      	30  SwiftUI                             0x000000010f06f759 objectdestroy.330Tm + 2393
      	31  SwiftUI                             0x000000010e8051b5 objectdestroy.30Tm + 2197
      	32  SwiftUI                             0x000000010e8051f5 objectdestroy.30Tm + 2261
      	33  libswift_Concurrency.dylib          0x00000001b1660445 _ZL23completeTaskWithClosurePN5swift12AsyncContextEPNS_10SwiftErrorE + 1
      )
      libc++abi: terminating due to uncaught exception of type NSException
      

      Can you reproduce the bug?

      Sometimes

      Reproduction Steps

      extension Realm {
          public func get<O: Object & Identifiable>(_ object: O.Type, id: O.ID) throws -> O
          {
              let ticket = UUID().uuidString
              debugLog("[Realm] [\(ticket)] Retrieving [\(String(describing: O.self))] with id [\(id)]")
              
              guard let result = self.object(ofType: O.self, forPrimaryKey: id) else { // <-- crash here
                  throw Error.objectNotFoundError
              }
              
              debugLog("[Realm] [\(ticket)] Finished")
              return result
          }
      }
      

      Note that objects' id properties (from the Identifiable conformance) are also the primary keys from a Realm Object perspective. (Otherwise this wouldn't work).

      Version

      10.40.0

      What Atlas Services are you using?

      Local Database only

      Are you using encryption?

      No

      Platform OS and version(s)

      16.4

      Build environment

      Xcode version: ...
      Dependency manager and version: ...

            Assignee:
            Unassigned Unassigned
            Reporter:
            unitosyncbot Unito Sync Bot
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: