Adding an Object to Synced Realm

Dear gents,
For better understanding I will take as example the TaskApp that is in the tutorials.
I have small issues adding a Project to the Realm.

App flow :

  1. User Login
  2. Main view with all the projects.
  3. Adds a new project.
    Here first thing I’m adding the project data to the memberOf array of userRealm file.
    Second create an actual Project RealmObject that I need to add to Cloud. When I use the userRealm.write i get the following error :

I understand the meaning, as I’m trying to add an object with different partition than the one opened, but how can I add it to the global realm (other than using backend functions, as the there is a lot of variables to input to that function)?
What I’m I doing wrong ? :smiley:

Anyone ?

The problem itself seems easy to understand, I guess, the solution is rather unintuitive.

I need to add a new partition to the Realm, same time to add the object to that partition. The difficulties are that I want to add that object when the user is fully committed to add it as when he hits Add Button, otherwise I should not create it.

The solution that I though was to create a backend function to add a document based on the new project partition and the object data. The part that I found it difficult how I pass the full object to the function as it contains a lot of variables and other embedded objects.

Please help :smiley:

Thanks you.

Hi @Radu_Patrascu,

You cannot add a partition to a Realm: a realm is the local representation of a partition, all objects in it must have the same partition value in their partition key variable. If you want to create a new partition, you need to open a new realm, configured to use the partition value you want to create. Adding objects with the right partition value to it is just a matter of using realm.write {…} on the realm you just created.

Hope this helps!

Thank you Paolo for replying.

I know I cannot add a new partition to the opened realm, what I’m asking is how I add an object with a different partition to the cluster “Project” collection, without user a super user function. I will rethink the logic to something else or try different db.

On the MongoDB backend, a partition doesn’t really exist, it’s just a value in a field that you define, that will filter what the client will receive: when, on the client side, you open a realm with a partition value that doesn’t exist yet, the first object you add will effectively create the partition. Now, what you may be facing is that you have set permissions in Sync that don’t give your user access to anything else than a specific partition, but that’s a different issue…

That actually the issue, cause I need to temporary give permission to open that partition, then if user cancels in mid process of adding a new object, I need to track that no object actually created and need to delete the permissions. Seems too complicated and it will definitely not work in the offline mode.

Maybe you can describe what your use case is, and how you want to achieve it? There may be different solutions, either involving custom functions for the permissions, or not requiring different partitions at all…

I figured it out in the end, used the RChat example, but got now in different problem, see below:

I’m getting Error [-1000] after user is adding a photo to realm. First login works fine, as soon as I update the photo, doesn’t want to load it anymore. Can you help ?
And in logs:

Basically not identifying the login user, what is happening ?

Unfortunately this is still not enough for a proper diagnosis: the screen shot just shows a successful login, so it doesn’t tell much about the error you’re experiencing (that, according to the code, doesn’t seem to be a Realm SDK error). For a proper investigation, you may want to share additional info, that however may not be appropriate to put in a public forum: if you have a Support Contract, we could follow you more closely by opening a case there.

Yeah, part of not showing too much code is due to that.
I tried to open a case, but says not available for me, don’t have a paid plan yet, not till the app is ready for production, any suggestions to get help free ? Or I should just start a plan for the support ?

Hi @Radu_Patrascu,

The Support plans are independent from the paid Atlas plan, you can find them in the Support page of your Atlas organisation, and there’s a trial option.