Skip to content

Android: Cannot create asynchronous query while in a write transaction #3889

Open
@cristianoccazinsp

Description

Goals

Listen to a collection.

Expected Results

Collection listener to succeed

Actual Results

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

Steps to Reproduce

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).

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

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions