realm / realm-js

Realm is a mobile database: an alternative to SQLite & key-value stores
https://realm.io
Apache License 2.0
5.74k stars 566 forks source link

useObject: causing re-render continously #6861

Open yashsway opened 2 weeks ago

yashsway commented 2 weeks ago

How frequently does the bug occur?

Always

Description

The underlying schema I'm referencing in useObject and the primary key are not changing at all which I've verified but the hook still returns brand new objects from the DB every few seconds.

I've had to solve this by manually writing the query using this alternative instead:

const data = useMemo(() => realm.objectForPrimaryKey(MySchema, variableWithId), [variableWithId]);

According to the docs the hook is supposed to only update on changes to the properties on the returned object?

The hook will update on any changes to the properties on the returned object and return null if it either doesn't exists or has been deleted.

I've verified that isn't the case. The properties are being read from only - not modified.

Stacktrace & log output

No response

Can you reproduce the bug?

Yes

Reproduction Steps

  1. Have a valid Realm DB with a large Realm schema with some data
  2. Use useObject() in a React component
  3. Observe component re-render continously

Version

0.6.1

What services are you using?

Local Database only

Are you using encryption?

No

Platform OS and version(s)

realm 12.3

Build environment

react-native: 0.73.6 expo: 50.0.0 react: 18.2.0 react-dom: 18.2.0

Cocoapods version

No response

sync-by-unito[bot] commented 2 weeks ago

➤ PM Bot commented:

Jira ticket: RJS-2895

kraenhansen commented 2 weeks ago

What are you passing as arguments to your call of useObject? Please provide a full example.

yashsway commented 2 weeks ago

What are you passing as arguments to your call of useObject? Please provide a full example.

@kraenhansen

This is in a react native app using Expo. Here's the component in which I was using the hook. This problem occurs everywhere where I use this hook but I'll just provide a snippet of one component...


const CreateFormSubmission = ({
  route: {
    params: {
      taskId,
    },
  },
  navigation,
}: FormStackScreenProps<"CreateFormSubmission">) => {
  // excluding other state here for brevity
  const cachedTask = useObject(Task, taskId ?? ""); // taskId doesn't change here - it's just a route param

  // ... excluded for brevity
  return <>some react native code</>;
};

where Task is a Realm schema that looks like:

export class Task extends Object<Task> {
  id!: string;
  cache_id!: string;
  cached_at?: Date;
  company_id!: string;
  facility_id!: string;
  assignee_user?: string;
  assignee_group?: string;
  name!: string;
  description!: string;
  status!: "complete" | "open" | "dismissed";
  type!: "manual_task" | "submit_form" | "complete_event";
  workflow!: WorkflowEmbedded;
  workflow_id?: string;
  form_submission?: FormSubmissionEmbedded[];
  config_form_schema?: FormSchemaEmbedded;
  emission_event?: ConfigEmissionEvent[];
  step_id!: string;
  created_at!: Date;

  static schema: ObjectSchema = {
    name: "Task",
    properties: {
      id: "string",
      cache_id: "string",
      cached_at: {
        type: "date",
        default: () => new Date(),
      },
      name: "string",
      assignee_user: "string?",
      assignee_group: "string?",
      description: "string",
      status: "string",
      type: "string",
      workflow: "WorkflowEmbedded",
      workflow_id: "string?",
      form_submission: "FormSubmissionEmbedded[]",
      config_form_schema: "FormSchemaEmbedded?",
      emission_event: "ConfigEmissionEvent[]",
      step_id: "string",
      created_at: "date",
    },
    primaryKey: "id",
  };
}
kraenhansen commented 2 weeks ago

You're writing that you're using version "0.6.1" is that of @realm/react? I wonder if you've accidentally upgraded to use 0.8.0 or above and might be hitting https://github.com/realm/realm-js/issues/6842 which I just released a fix for in @realm/react@0.10.1.

yashsway commented 20 hours ago

@kraenhansen

yes @realm/react ! specific version set is ^0.6.1 (actual installed version then seems to be 0.6.2). sorry that wasn't clear!

ah interesting...thanks for linking me to that. We get this issue quite often too (we have error logging through Sentry) so perhaps upgrading should fix the issue.