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

Flatlist accessing object of type X which has been invalidated or deleted #6872

Open
susonthapa opened this issue Aug 30, 2024 · 1 comment

Comments

@susonthapa
Copy link

How frequently does the bug occur?

Sometimes

Description

We have switched to @realm/react and we are running into intermittent crashes. The issues are very similar to #1031. We are getting an Accessing object of type X which has been invalidated or deleted error. This is a pseudo-code of what we are doing.

const items = useQuery(List)

useEffect(() => {
   async function fetchData() {
      // fetch the List from the server and update
   }
   fetchData()
}, [])

return <Flatlist data={items} renderItem={renderItem} />

I want to mention that List has an embedded Realm object as well. The crash tends to happen when we are rendering a large number of items and scrolling at the same time while we are fetching the list from the server.

Judging by the error, we are accessing old Realm objects within the renderItem function which was quite surprising since we are using @realm/react and expected it to take care of this kind of issue.

I noted this from the React Native docs.
image

In order to constrain memory and enable smooth scrolling, content is rendered asynchronously offscreen. This means it's possible to scroll faster than the fill rate and momentarily see blank content.

I think the asynchronous rendering of the Flatlist is the culprit. This is what I think is happening but I might be wrong.

  1. The initial data is rendered.
  2. Flatlist is deferring the rendering of items not visible on the screen.
  3. fetchData is called and the Realm is updated.
  4. The user scrolls the list causing Flatlist to access the invalidated object.
  5. useQuery schedules a rerender(the app will crash by the time this happens).

Are you guys aware of these issues? Is there any recommendation on how to use Realm with Flatlist?

For the time being, we are using isValid to check within the renderItem prop before accessing the Realm object but this doesn't look right.

Stacktrace & log output

No response

Can you reproduce the bug?

Sometimes

Reproduction Steps

No response

Version

realm: 12.6.0, @realm/react: 0.6.2

What services are you using?

Local Database only

Are you using encryption?

Yes

Platform OS and version(s)

Android 14

Build environment

React Native: 0.73.6
New Architecture: false
Engine: Hermes

Cocoapods version

No response

Copy link

sync-by-unito bot commented Aug 30, 2024

➤ PM Bot commented:

Jira ticket: RJS-2897

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant