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/react - Cannot create asynchronous query while in a write transaction #4306

Closed
WenLonG12345 opened this issue Feb 4, 2022 · 6 comments
Labels
O-Community T-Bug Waiting-For-Reporter Waiting for more information from the reporter before we can proceed

Comments

@WenLonG12345
Copy link

WenLonG12345 commented Feb 4, 2022

How frequently does the bug occur?

All the time

Description

Hi, I am recently using the new hooks library @react/realm, but I get some error when using it with redux saga.

My situation is to call API to get a list of room, insert it to realm and display in UI.

Here is my code.

const Room = ({}) => {
 
 const roomList = useQuery('Room');

 const realm = useRealm();  <-- from RealmContext
 
 useEffect(() => {
    dispatch({ type: 'roomModel/getRoomList', realm }); <-- dispatch action to sagas
  }, []);
}
  *getRoomList({realm}, { put, call }) {
    const { roomType } = constant;

    const response = yield call(roomService.getRoomList, { roomType });

    if (response) {
      const { status, data } = response;

      if (status === 200) {
        const { rooms } = data;

        rooms.map(room => {
      
          realm.write(() => {
            realm.create('Room', room, "modified");
          });
        });
      }
    }
  }

Sagas is responsible to perform the insert operation, but somehow I keep getting the error on Cannot create asynchronous query while in a write transaction.
I was wondering if it is a race condition on these hooks library?

By the way, I am still beginner in React, do correct me if I made any mistake.

Stacktrace & log output

Error: Cannot create asynchronous query while in a write transaction

Can you reproduce the bug?

Yes, always

Reproduction Steps

No response

Version

10.11.0

What SDK flavour are you using?

Local Database only

Are you using encryption?

No, not using encryption

Platform OS and version(s)

Android 11

Build environment

Which debugger for React Native: ..
Flipper

Cocoapods version

No response

@takameyer
Copy link
Contributor

@WenLonG12345 Strange issue. It does feel like a race condition. What would help us investigate would be a sample project that we could run that causes this issue. Can you provide that for us?

@sync-by-unito sync-by-unito bot added the Waiting-For-Reporter Waiting for more information from the reporter before we can proceed label Feb 4, 2022
@WenLonG12345
Copy link
Author

Hi sorry for the delay. I am changing hooks approach to use the traditional way, but yet still get the same error.

https://github.com/WenLonG12345/RealmRaceCondition

Here is my example on implementing realm, and the error occurs when onAdd() to adding new value to db, the addListener seems throwing this async query error.

Or maybe I did a wrong approach?

@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 Feb 17, 2022
@takameyer
Copy link
Contributor

takameyer commented Feb 17, 2022

I would recommend using the hooks approach for querying your data. This will make sure the listeners are correctly persisted. You can still use your same approach for writing data. As I said in the other thread, opening realm with the same config will just give you a shared instance.

const roomList = useQuery('Room');

@WenLonG12345
Copy link
Author

Hi, I had changed to hooks approach, yet the error Cannot create asynchronous query while in a write transaction still occurs. Any idea why?

image

@takameyer
Copy link
Contributor

@WenLonG12345 I have made a PR on the repository you sent me. This is working with no errors in both Android and iOS.
https://github.com/WenLonG12345/RealmRaceCondition/pull/1/files

@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 Feb 18, 2022
@tomduncalf
Copy link
Contributor

This sounds like it could be related to or a duplicate of #4375.

@WenLonG12345 are you still having this problem? If so please feel free to reopen this issue with more details.

@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.
Labels
O-Community T-Bug Waiting-For-Reporter Waiting for more information from the reporter before we can proceed
Projects
None yet
Development

No branches or pull requests

3 participants