Uploaded image for project: 'Realm JavaScript SDK'
  1. Realm JavaScript SDK
  2. RJS-1285

Android: Cannot create asynchronous query while in a write transaction

      <!---

      Questions: If you have questions about HOW TO use Realm, please ask on
      StackOverflow: http://stackoverflow.com/questions/ask?tags=realm
      We monitor the realm tag.

      Feature Request: Just fill in the first two sections below.

      Bugs: To help you as fast as possible with an issue please describe your issue
      and the steps you have taken to reproduce it in as much detail as possible.

      -->

      Goals

      <!--- What are you trying to achieve? -->
      Listen to a collection.

      Expected Results

      <!--- What did you expect to happen? -->
      Collection listener to succeed

      Actual Results

      <!--- What happened instead? -->
      <!--- e.g. the stack trace of a crash -->

      Error while trying to listen to a collection: Cannot create asynchronous query while in a write transaction

      Steps to Reproduce

      <!--- What are steps we can follow to reproduce this issue? -->

      Code Sample

      The log happens as follows. I'm adding the helper functions for clarity as well.

      export function asyncQuery(realm, cb) {
        return new Promise((resolve, reject) => {
          try {
            if (!realm.isInTransaction) {
              resolve(cb(realm));
            } else {
              //console.warn("Realm (query) was already in a transaction, delaying call one more loop.");
              setTimeout(() => {
                try {
                  resolve(cb(realm));
                } catch (err) {
                  reject(err);
                }
              });
            }
          } catch (err) {
            reject(err);
          }
        });
      }
      
      export async function getQueueCollection() {
        return await asyncQuery(global.realm, (realm) => {
          return realm.objects('QueueItem');
        });
      }
      
      try {
        // CRASH HERE:
        this.queueListener = await getQueueCollection();
        this.queueListener.addListener(this.handleRealmUpdate);
      } catch (err) {
        console.error(
          'CRITICAL Queue: Failed to listen to queue collection: ',
          err.message || err,
        );
        Alert.alert(
          'Error',
          'There was an unexpected error and the app needs to be restarted.',
        );
        throw err;
      }
      

      Issue started happening somewhere between 10.5.x and 10.6.1 (did not happen in 10.4.x).

      <!---
      Please provide a code sample or test case that highlights the issue.
      If relevant, include your model definitions.
      For larger code samples, links to external gists/repositories are preferred.
      Full projects that we can compile and run ourselves are ideal!
      -->

      Version of Realm and Tooling

      • Realm JS SDK Version: 10.6.1
      • Node or React Native: React Native 0.62.2
      • Client OS & Version: Android 11 / Pixel 5
      • Which debugger for React Native: None / Production

            Assignee:
            Unassigned Unassigned
            Reporter:
            unitosyncbot Unito Sync Bot
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: