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

useQuery always returns referentially different objects #4913

Closed
mordechaim opened this issue Sep 14, 2022 · 5 comments
Closed

useQuery always returns referentially different objects #4913

mordechaim opened this issue Sep 14, 2022 · 5 comments
Assignees

Comments

@mordechaim
Copy link

mordechaim commented Sep 14, 2022

How frequently does the bug occur?

All the time

Description

According to the comments in the following code snippet:

// Wrap the cachedObject in useMemo, so we only replace it with a new instance if `primaryKey` or `type` change
const { collection, tearDown } = useMemo(
() => createCachedCollection({ collection: realm.objects(type), realm, updateCallback: forceRerender }),
[type, realm],
);

Which relies on cached collections:

/**
* Creates a proxy around a {@link Realm.Collection} that will create new {@link Realm.Object}
* references on any relevant change (update, insert, deletion) and return the same
* object reference if no changes have occurred since the last access.

The result of useQuery should remain referentially the same so we could use it as a dependency for useMemo and friends. I cleary observe otherwise, on each render the returned collection is different and breaks my memoization.

Stacktrace & log output

No response

Can you reproduce the bug?

Yes, always

Reproduction Steps

Given a schema Cat

const Demo = () => {
    const cats = useQuery('Cat')
    useMemo(() => console.log('memo busted'), [cats])

    // force a render
    const [count, setCount] = useState(0)
    return <Pressable onPress={e => setCount(c => c + 1)}>
        <Text>Increment</Text>
    </Pressable>
}

The console will output memo busted on each press.

Version

11.0.0-rc.0

What SDK flavour are you using?

Local Database only

Are you using encryption?

No, not using encryption

Platform OS and version(s)

Android 11

Build environment

System:
    OS: Windows 10 10.0.19044
    CPU: (8) x64 Intel(R) Core(TM) i7-4790 CPU @ 3.60GHz
    Memory: 21.20 GB / 31.91 GB
  Binaries:
    Node: 18.8.0 - C:\Program Files\nodejs\node.EXE
    Yarn: 1.22.19 - ~\AppData\Roaming\npm\yarn.CMD
    npm: 8.18.0 - C:\Program Files\nodejs\npm.CMD
    Watchman: Not Found
  SDKs:
    Android SDK:
      Android NDK: 21.4.7075529
    Windows SDK: Not Found
  IDEs:
    Android Studio: AI-212.5712.43.2112.8815526
    Visual Studio: Not Found
  Languages:
    Java: 11.0.12 - /c/Program Files/Android/Android Studio/jre/bin/javac
  npmPackages:
    @react-native-community/cli: Not Found
    react: 17.0.2 => 17.0.2
    react-native: 0.68.2 => 0.68.2
    react-native-windows: Not Found
  npmGlobalPackages:
    *react-native*: Not Found

Cocoapods version

No response

@kneth
Copy link
Member

kneth commented Sep 15, 2022

@mordechaim Thank you for reporting. You are right, and similar behaviour has been reported in #4513. We need to wrap up #4519 to get a fix in.

@kneth kneth mentioned this issue Sep 15, 2022
@mordechaim
Copy link
Author

@kneth thank you.

As an aside: with this issue fixed, will filtered collections also remain referentially the same object given the same filters?

So if I do:

const brownCats = useQuery('Cat').filtered('color == "brown")

Will the memo be retained?

@kneth
Copy link
Member

kneth commented Sep 15, 2022

For now, you will have to do

const cats = useQuery('Cat');
const brownCats = useMemo(cats.filtered('color == "brown"), [cats]);

@takameyer
Copy link
Contributor

@mordechaim Thanks for reporting this. We have it planned and hopefully will get around to merging the fix to this soon. We just need to write regression tests to make sure this doesn't happen again.

I am closing this issue there is already a related issue and a PR.

@channeladam
Copy link

Argh, same. Just stumbled upon this issue after creating #5264 .

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 15, 2024
@sync-by-unito sync-by-unito bot changed the title useQuery always returns referentially different objects useQuery always returns referentially different objects Mar 15, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants