Realm not giving constant results

Hey, I have a problem that is frustrating me for a long time. It’s a bit long but if you can read that I will be more than thankful.

I have an app, that has users, and each user can create his own “card” which is basically like a digital business card.

The partition of the cards is in the following format: “card=userID”

So I want to be able to share those cards between users. To do so, I am generating a link, that consists of the user ID. So when the app opening the link it just needs to create a new Realm instance with the userID in the partition to get the shared card.

And here the problem starts. I’ve noticed that when I search for cards, it will give a result of 0, meaning a result with no cards, unless the user that created the following partition and card is the one trying to access it. then it does give a result other than 0.

Permissions on the website, in sync settings, are set to “true” for both “read” and “write”.

I’ve also noticed that sometimes it is indeed able to find a card, that the logged user hasn’t created but that’s rare, and makes no sense. why would it sometimes work and sometimes won’t?

Do you know what can the problem be? why it can’t find cards that were not created by the logged user?

Thanks to everyone who read this.

How are you opening the other realm/partition? Realm always makes the queries on the local DB copy, that may or may not be fully synchronised with the remote content. If you’re looking at a different user’s data, there may be the need to download the partition itself to a local copy before having it ready, if you don’t give the process enough time to elaborate the remote contents that come in, you won’t get any result. If you’re the user creating the card, then the local DB already contains the reference to it, but a realm updated somewhere else will take time to propagate the changes to you.

1 Like

Thank you so much for answering!
I understand where the problem can come from.
I tried now to make it download a local copy, but I can’t find how to do that. Can you please let me know how to download the partition?
Thank you!

Well, it depends which SDK are you using, can you please share this detail?

Android SDK.
I’ve imported to the project the following: classpath “io.realm:realm-gradle-plugin:10.7.0”

That’s all I can tell about the sdk

if that helps more, I’ve been learning based on that tutorial -

So I am using the same sdk as here

Hey Ron,

That quick start tutorial will get you up and running but necessarily omits a lot of details. For your case, I think you’ll want to adjust the code that opens the realm + the configuration that you pass to it. Check out the Open a Synced Realm usage example for some more guidance.

Specifically, you’ll want to:

  • Configure Realm to sync all data before completing the operation (with the waitForInitialRemoteData() builder method)
  • Consider using getInstanceAsync() instead of getInstance() since downloading all the initial data will take more time and will block your UI thread if you open the realm synchronously there.

Hopefully this solves your inconsistent results issue!

Hey, thanks for your answer!
I am now not near my computer anymore, I will try it as soon as I can.
Thanks for the guidance

waitForInitialRemoteData() is the way to download the partition itself to a local copy, as Paolo suggested?


Hey Nick,

I’ve tried your suggestion and I haven’t encountered any issues now so it seems to work perfectly! Thank you very much for your help! And thanks @Paolo_Manna for guiding me as well!

1 Like

Awesome glad to hear everything is working for you! :slightly_smiling_face:

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.