Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

useObject doesn't react to the object being inserted #4514

Closed
mfbx9da4 opened this issue Apr 21, 2022 · 6 comments · Fixed by #5208
Closed

useObject doesn't react to the object being inserted #4514

mfbx9da4 opened this issue Apr 21, 2022 · 6 comments · Fixed by #5208
Assignees

Comments

@mfbx9da4
Copy link

mfbx9da4 commented Apr 21, 2022

How frequently does the bug occur?

All the time

Description

As a user, if I do the following const foo = useObject('foo', fooId) and foo is initially not in the database, I would expect the hook to re-render if the object with fooId is later inserted into realm. This is how most state libraries work in react. In practice, the way useObject works is that it will not re-render. The way to solve this is to do the following in the useObject hook:

Try to get the object by primary key. If it exists setup a listener. Otherwise listen to its collection. In the collection listener callback, if the object has been inserted, setup the listener.

Stacktrace & log output

No response

Can you reproduce the bug?

Yes, always

Reproduction Steps

No response

Version

main

What SDK flavour are you using?

Local Database only

Are you using encryption?

No, not using encryption

Platform OS and version(s)

ios

Build environment

No response

Cocoapods version

No response

@takameyer
Copy link
Contributor

@mfbx9da4 Yes, this is indeed a problem. We will look into it and get back to you when we have a fix.

@mordechaim
Copy link

mordechaim commented May 4, 2022

@takameyer
Until this is fixed, is there a react-friendly workaround?

EDIT: So this is my workaround which does follow new inserts:

// replace this
const cat = useObject('Cat', 12)

// with
const cat = useQuery('Cat').filtered('id == 12')[0]

@takameyer
Copy link
Contributor

takameyer commented May 5, 2022

@mordechaim That is similar to the workaround I would've suggested. Thanks for sharing!

The solution to this problem will be something similar. We will have to register a listener on the filtered parent collection and update the state when the object appears.

@channeladam
Copy link

Argh, just ran into this issue :(

takameyer added a commit that referenced this issue Dec 30, 2022
* Prime any list properties with an cachedCollection so that updates fire correctly (Fixes #5185)
* Primary Keys as non-primative values would reset the cached objects, since their reference always changes
* Create a listener on the collection if the object doesn't exist, and rerender when it is created (Fixes #4514)
@takameyer takameyer mentioned this issue Dec 30, 2022
11 tasks
takameyer added a commit that referenced this issue Dec 30, 2022
* Prime any list properties with an cachedCollection so that updates fire correctly (Fixes #5185)
* Primary Keys as non-primative values would reset the cached objects, since their reference always changes
* Create a listener on the collection if the object doesn't exist, and rerender when it is created (Fixes #4514)
@takameyer
Copy link
Contributor

The PR #5208 will fix this issue. Now, when the object does not exist, there will be a listener established on the collection that will re-render when an object with the given primary key comes into existence.

@Fuzzyma
Copy link

Fuzzyma commented Nov 29, 2023

@takameyer I still have this issue!
When deleting and recreating an object, the deletion is handled properly and the object becomes null. However after reinsertion it stays null.
The temporary fix of @mordechaim worked for me

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 14, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants