Uploaded image for project: 'Realm JavaScript SDK'
  1. Realm JavaScript SDK
  2. RJS-2487

`useObject` sometimes is not reacting to changes on the primary key

      How frequently does the bug occur?

      Sometimes

      Description

      Hi!

      I apologize if there is something wrong with the way I'm using the hooks provided by @realm/react, but it looks like there is something missing in the useObject hook to properly react to changes on the primary key.

      This is my use case:

      In a React component I'm calling useObject with a primary key (string) which is a global state. I'm calling it like that:

      const stateId = useRecoilValue(userStateId);
      const currentState = useObject<State>('State', stateId || '');
      console.log(stateId, '->', currentState?.id);
      

      In another view I change the value of the state. Everytime I make a change in the value it react to changes, but the useObject hook is not always reacting to that change. The logs I'm getting from the first component is this:

      LOG: tanking -> tanking
      LOG: to the base -> to the base
      LOG: having lunch -> having lunch
      LOG: available -> available
      LOG: tanking -> having lunch
      LOG: available -> available
      

      As you can see, there is something bad with the logs I'm getting, when I changed the state to tanking the Realm.Object does not change.
      I checked on the code of the @realm/react package and I found a solution (at least for my use case) so I use patch-package in order to solve it in my app. This is what I changed in node_modules/@realm/react/dist/index.js:

      Unable to find source-code formatter for language: patch. 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
      --- a/node_modules/@realm/react/dist/index.js
      +++ b/node_modules/@realm/react/dist/index.js
      @@ -356,6 +356,8 @@ function createUseObject(useRealm) {
                           updatedRef,
                       });
                       originalObjectRef.current = originalObject;
      +                primaryKeyRef.current = primaryKey;
      +                updatedRef.current = true;
                   }
                   return cachedObjectRef.current;
               }, [realm, originalObject, primaryKey]);
      

      Now, if it is useful, this is what I think is happening here:

      In @realm/react/src/useObject.tsx:73 there is a useMemo that returns the object for the primary key, at line 84 there is a check (!arePrimaryKeysIdentical(primaryKey, primaryKeyRef.current)) that resets the cached value, but there is no call for something like primaryKeyRef.current = primaryKey so the primaryKeyRef keeps the initial value (in my case 'tanking') and when we get back to that value it thinks that the actual value of the primary key has not changed.

      Another thing I had to do was to call updatedRef.current = true after primaryKeyRef.current = primaryKey in order to make it work, I could find a reason for that but without it is not working, I just find out that with the first change the object returned by that useMemo is the right Realm.Object, but, in the same file, at line 141 there is check for updatedRef.current and sometimes it had a false value and that times I was getting the old version of the Realm.Object, the one with the old primary key.

      Stacktrace & log output

      No response

      Can you reproduce the bug?

      Sometimes

      Reproduction Steps

      1. Call useObject with a primary key A.
      2. Change the value of A to something different.
      3. The Realm.Object returned will change (working fine).
      4. Change the value of A to the same initial value (the one used in the step 1).
      5. The Realm.Object returned will be the same before and after the last change on the PK (the one used in the step 4).

      Version

      Realm 11.10.1; @realm/react 0.5.1

      What services are you using?

      Local Database only

      Are you using encryption?

      No

      Platform OS and version(s)

      Ubuntu 22.04.2 LTS x86_64

      Build environment

      Node 14.21.3
      npm 6.14.18
      React Native 0.72.3

      Cocoapods version

      No response

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

              Created:
              Updated:
              Resolved: