realm-js icon indicating copy to clipboard operation
realm-js copied to clipboard

react sync problems with 12.0.0-browser.2

Open jamalsoueidan opened this issue 6 months ago • 3 comments

How frequently does the bug occur?

Sometimes

Description

useQuery is not forcing react components to re-render after changing, updating a array property, sometime it works, sometime it doesnt..

I have my types that look like this and im trying to update user_ids.

export type Conversation = {
  _id: Realm.BSON.ObjectId;
  business_phone_number_id: string;
  customer_phone_number: string;
  name?: string;
  timestamp: number;
  user_ids: Realm.List<string>;
};

export const ConversationSchema = {
  name: "Conversation",
  properties: {
    _id: "objectId",
    business_phone_number_id: "string",
    customer_phone_number: "string",
    name: "string?",
    timestamp: "double",
    user_ids: "string[]",
  },
  primaryKey: "_id",
};

In one place I do this:

realm.write(() => {
    realm.create(
      "Conversation",
      {
        _id: conversation?._id,
        user_ids: selectedUserIds,
      },
      Realm.UpdateMode.Modified
    );
  });    

and where I show the data I use this hook.

I tried to use push and remove, didnt make a difference, the changes is applied in the mongodb db, but not in the view, I can see its uploading and downloading the data using realm.syncSession.

export function useGetConversation(conversationId?: string) {
  const conversations = useQuery<Conversation>(
    ConversationSchema.name,
    (collection) =>
      collection.filtered(
        "_id = $0 LIMIT(1)",
        new Realm.BSON.ObjectId(conversationId)
      ),
    [conversationId]
  );

  return conversations[0];
}

The data does not always gets updated in the view, whenever realm.write execute.

I also tried to use, doesn't work.

const conversation = realm.objectForPrimaryKey<Conversation>(
    ConversationSchema.name,
    objectId
  );

I can see the hook gets re-rendered, but not in the view.

BUT if I add after the useQuery

  console.log("updated, conversations", conversations[0].user_ids.length); // <<<<<< after the useQuery

Then react can always see that the data have changed, and will update the view.

Stacktrace & log output

No response

Can you reproduce the bug?

Always

Reproduction Steps

As shown above.

Version

12.0.0-browser.2

What services are you using?

Atlas Device Sync

Are you using encryption?

No

Platform OS and version(s)

Windows

Build environment

Which debugger for React Native: ..

Cocoapods version

No response

jamalsoueidan avatar Aug 23 '24 12:08 jamalsoueidan