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

Realm syncing incomplete data #6031

Closed
njmulsqb opened this issue Aug 2, 2023 · 7 comments
Closed

Realm syncing incomplete data #6031

njmulsqb opened this issue Aug 2, 2023 · 7 comments

Comments

@njmulsqb
Copy link

njmulsqb commented Aug 2, 2023

How frequently does the bug occur?

  • select --

Description

Hi, I am using realm on react native expo.

The issue is that when I start the app, there are some subscriptions which loads the data, but the data loaded is incomplete, and when I insert some data into the same collection, realm then syncs all the data including the newly inserted one.

Why isn't it syncing all the data at first place?

Here are some code snippets to help with troubleshooting

## App.tsx (Realm initialization code)

 <AppProvider id={process.env.REALM_APP_ID}>
      <UserProvider fallback={realmLogIn}>
        <RealmProvider
          sync={{
            flexible: true,
            onError: console.error,
            initialSubscriptions: {
              update(subs, realm) {
                subs.add(realm.objects("realmTest"));
                //subs.add(realm.objects("users"));
              },
            },
          }}
        >

## Getting all the data using subscriptions

  useEffect(() => {

    // Subscription for fetching the connections
    realm.subscriptions.update((mutableSubs, realm) => {
      const connectionsQuery = realm
        .objects("connection")
        .filtered("user = $0 OR friend = $0", new Realm.BSON.ObjectID(userId));
      mutableSubs.add(connectionsQuery, { name: "connections" });
    });

    // Subscription for fetching the users
    realm.subscriptions.update((mutableSubs, realm) => {
      const connections = realm.objects("connection");
      const userIds = Array.from(
        new Set(
          connections.reduce((ids, connection) => {
            ids.push(connection.user, connection.friend);
            return ids;
          }, [])
        )
      );

      const usersQuery = realm.objects("users").filtered("_id IN $0", userIds);
      mutableSubs.add(usersQuery, { name: "users" });
    });
    // Subscription to fetch the groups
    realm.subscriptions.update((mutableSubs, realm) => {
      const connections = realm.objects("connection")
      const groupIds = Array.from(
        new Set(
          connections.reduce((ids, connection) => {
            if (connection.groupExp) {
              connection.groupExp.forEach((group) => {
                ids.push(group.groupId);
              });
            }
            return ids;
          }, [])
        )
      );
      const groupsQuery = realm
        .objects("group")
        .filtered("_id IN $0", groupIds);
      mutableSubs.add(groupsQuery, { name: "groups" });
    });

    // Subscription to fetch the expenses
    realm.subscriptions.update((mutableSubs, realm) => {
      const connections = realm.objects("connection");

      const filteredExpenseIds = connections.reduce(
        (filteredIds, connection) => {
          if (connection.nonGroupExp) {
            connection.nonGroupExp.forEach((expense) => {
              filteredIds.push(new Realm.BSON.ObjectID(expense.expenseId));
            });
          }
          return filteredIds;
        },
        []
      );

      const filteredGroupIds = connections.reduce((filteredIds, connection) => {
        if (connection.groupExp) {
          connection.groupExp.forEach((group) => {
            filteredIds.push(new Realm.BSON.ObjectID(group.groupId));
          });
        }
        return filteredIds;
      }, []);

      const expensesQuery = realm
        .objects("expense")
        .filtered(
          "_id IN $0 OR groupId IN $1",
          filteredExpenseIds,
          filteredGroupIds
        );
      mutableSubs.add(expensesQuery, { name: "expenses" });
    });

    // Removing the initial subscription
    realm.subscriptions.update((mutableSubs) => {
      mutableSubs.removeByObjectType("realmTest");
    });

    // realm.subscriptions.update((mutableSubs) => {
    //   mutableSubs.removeAll();
    // });

  });

## Fetching the data in groups.tsx
const groups = realm.objects("group");

## and here's the code that inserts the object
  realm.write(() => {
        let realmObject = realm.create("group", {
          _id: new Realm.BSON.ObjectId(),
          name: values.name,
          members: [
            {
              hasJoined: true,
              isManager: true,
              userId: realmUser?._id,
            },
          ],
          isPinned: false,
          isArchived: false,
          defaultCurrency: currency?.currency_short_form,
          description: "",
          settlementCycle: { repeatAfterDays: 7, startingFrom: date },
          createdAt: new Date(),
          dueDate: dueDate,
          inviteId: inviteId,
          isSettledUp: false,
        });
        setNewGroup(realmObject);
        console.log(newGroup)
      });

Stacktrace & log output

No response

Can you reproduce the bug?

Always

Reproduction Steps

Just do something that wipes the current session i.e. log out, remove realm files from explorer, remove all subscriptions and reinitialize, and you'll be having incomplete data.

Add some data to realm using realm.write and whole collection will be synced then.

Version

Realm 11.9.0, @realm/react 0.5.1

What services are you using?

Both Atlas Device Sync and Atlas App Services

Are you using encryption?

No

Platform OS and version(s)

React Native Expo 48.0.0

Build environment

Which debugger for React Native: ..

Cocoapods version

No response

@takameyer
Copy link
Contributor

@njmulsqb Thanks for submitting this issue and sorry you are facing issues. Can you provide an example of the data that isn't showing up? What specific subscription is failing in the provided case?
It would be helpful to see which data value is causing the issue against which subscription.

@sync-by-unito sync-by-unito bot added the Waiting-For-Reporter Waiting for more information from the reporter before we can proceed label Aug 3, 2023
@njmulsqb
Copy link
Author

njmulsqb commented Aug 3, 2023

@takameyer Thanks for the response,

groups subscription is failing to sync properly here. Here's the schema of the group susbcription

import Realm from "realm";

class Group extends Realm.Object {
  static schema = {
    name: "group",
    properties: {
      _id: "objectId",
      defaultCurrency: "string?",
      description: "string?",
      dueDate: "date?",
      createdAt: "date?",
      inviteId: "string?",
      isArchived: "bool?",
      isPinned: "bool?",
      isSettledUp: "bool?",
      members: "group_members[]",
      name: "string",
      settlementCycle: "group_settlementCycle",
    },
    primaryKey: "_id",
  };
}
export default Group

The first three entries are only getting synced initially
image
and when I add some entry through realm.write all the rest of the data get synced then, one example of the rest of the data is as follows:
image

@github-actions github-actions bot added Needs-Attention Reporter has responded. Review comment. and removed Waiting-For-Reporter Waiting for more information from the reporter before we can proceed labels Aug 3, 2023
@takameyer
Copy link
Contributor

@njmulsqb Can you verify that the groupIds in this code:

      const groupsQuery = realm
        .objects("group")
        .filtered("_id IN $0", groupIds);

reflects the issue you are having? Are there missing groupIds?

@njmulsqb
Copy link
Author

njmulsqb commented Aug 3, 2023

Ah,
I think in the beginning I am missing groupIds, because it is being fetched from connections table where that ID is not present, but the question is, how are those groups being fetched after the creation then?

P.s. does realm has any official slack channel or discord server to discuss problems? The documentation is not so great and realm community is still maturing so it becomes challenging to solve realm related issues.

@takameyer
Copy link
Contributor

@njmulsqb I think it's more of a concurrency issue, since the connections are being subscribed to and read from in the same callback. What I would recommend is to break up your useEffect into multiple useEffect hooks and create explicit dependencies which will update the subscriptions.
I would not advise changing the subscriptions too often, rather to setup a longer living subscription of more data and filtering the local data.

P.s. does realm has any official slack channel or discord server to discuss problems? The documentation is not so great and realm community is still maturing so it becomes challenging to solve realm related issues.

GitHub is our primary. We do also have a community forum which is also being monitored by us and was designed with discussions in mind.

@njmulsqb
Copy link
Author

njmulsqb commented Aug 3, 2023

Thanks a bunch mate @takameyer

@njmulsqb njmulsqb closed this as completed Aug 3, 2023
@sync-by-unito sync-by-unito bot removed the Needs-Attention Reporter has responded. Review comment. label Aug 3, 2023
@takameyer
Copy link
Contributor

@njmulsqb Cheers and happy coding!

@github-actions github-actions bot locked as resolved and limited conversation to collaborators 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

2 participants