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

Issue when we use realmContext and Realm.config at same time #5871

Closed
somasekharkakarla opened this issue Jun 4, 2023 · 10 comments
Closed

Comments

@somasekharkakarla
Copy link

somasekharkakarla commented Jun 4, 2023

How frequently does the bug occur?

Always

Description

Hi @kneth and @takameyer ,

Here is a problem i have found with realm.

@realm/react is hook i am using in view components to for CURD operations.
I am using Realm.config in RealmMangerClass for listening the changes in old fashion and changed data was updated to server. I am writing the response from server to realm. These are all happing in RealmManager

In this way. i am initialising same config at two place. createRealmContext at App.js and Realm.open in RealmManager class

In random case both (RealmContext and Realm.open) > write to db.
This is causing two errors:

  1. realm already in write transaction
  2. [Error: Exception in HostFunction: Accessing object of type Tasks which has been invalidated or deleted]

Is there any way i can get reference of realm from createRealmContext. where i can use it in RealmManager class instead of initialising again in Class

I want to use hook on UI and listen to listener and do CURD operations in old fashion at same time.

Stacktrace & log output

No response

Can you reproduce the bug?

Always

Reproduction Steps

No response

Version

11.3.1

What services are you using?

Local Database only

Are you using encryption?

No

Platform OS and version(s)

android 13

Build environment

Which debugger for React Native: ..

Cocoapods version

No response

@takameyer
Copy link
Contributor

takameyer commented Jun 5, 2023

@somasekharkakarla The workflow you described is supported, so feel free to reinitialize it in your class. As long as you use the same configuration passed to createRealmContext and new Realm(), the result will be a shared instance of the same Realm. Even if you were to get the same realm from createRealmContext and have it in your RealmManager, the result would likely be the same.

Let's address the errors you are having:

realm already in write transaction

There is a member variable on the realm instance called isInTransaction. I would recommend using this in the areas that have been crashing to help ensure you do not come into conflict with realm transactions. Since JS is running on a single thread, using setImmediate can be helpful in ensuring the current write transaction finishes before the next happens by pushing it into the event loop:

   if (realm.isInTransaction) {
      setImmediate(() => {
        realm.write(() => {
          // Do something
        })
      })

[Error: Exception in HostFunction: Accessing object of type Tasks which has been invalidated or deleted]

This can happen if you are holding onto a realm object that isn't managed by the hooks (useObject, useQuery). The hooks will automatically re-render and nullify any invalid objects. If you are doing some logic with RealmManager to look at a held instance of some object, then I would recommend checking the member variable isValid on the object.

Let me know if this helps!

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

Hi @takameyer,

Thank for the response. isInTransaction is really helpful. Tell me something,
I am using a hook taskObj = useObject('Task', uid)
i delete it from backend from a async call. But i am still in same component. Then i am getting
[Error: Exception in HostFunction: Accessing object of type Tasks which has been invalidated or deleted]
i used isVaild() it says undefined and crash
is there anyway we can know the obj is deleted.

@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 Jun 5, 2023
@somasekharkakarla
Copy link
Author

Hi @takameyer ,
i'm pleasing you looking into this feature request #5864. I think it will give more power to developers

@takameyer
Copy link
Contributor

takameyer commented Jun 7, 2023

@somasekharkakarla Can you verify that the crash is happening within the component, or somewhere else? useObject will automatically return null when the object is not valid.

@somasekharkakarla
Copy link
Author

Hi @takameyer . i will reverify it again.

@somasekharkakarla
Copy link
Author

somasekharkakarla commented Jun 9, 2023

Hi @takameyer ,

i reverified. crash is happening at useObject only.

try {
taskObj = useObject('Tasks', task?.uid);
taskObj.addListener((task:any, changes:any) =>{
if(changes.deleted){
navigation.goBack();
}
})
} catch (e) {
console.error(" taskObj.addListener Error", e)

I am deleting this task by async process. while deleting i am staying in same screen.

@takameyer
Copy link
Contributor

takameyer commented Jun 13, 2023

@somasekharkakarla The code sample is a bit confusing. useObject should not be called within a try/catch and addListener should be inside of a useEffect, otherwise, you will be adding an additional listener on every re-render of this component.
Can you possibly provide a sample repo that replicates the crash you are getting?

@sync-by-unito sync-by-unito bot added Waiting-For-Reporter Waiting for more information from the reporter before we can proceed and removed Needs-Attention Reporter has responded. Review comment. labels Jun 13, 2023
@somasekharkakarla
Copy link
Author

Hi @takameyer , sure. i create separate repo for that share you soon.

@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 Jun 14, 2023
@kneth
Copy link
Contributor

kneth commented Jun 15, 2023

@somasekharkakarla Sounds good. We will leave the issue open and you can post a link to the repo once you are ready.

@sync-by-unito sync-by-unito bot added Waiting-For-Reporter Waiting for more information from the reporter before we can proceed Reproduction-Required and removed Needs-Attention Reporter has responded. Review comment. labels Jun 15, 2023
@sync-by-unito
Copy link

sync-by-unito bot commented Jun 29, 2023

➤ takameyer commented:

Closing due to inactivity. Please reopen when you can provide more information.

@sync-by-unito sync-by-unito bot closed this as completed Jun 29, 2023
@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

3 participants