0

I have an app that uses SQLite when I run on Android, seems to work fine when I do BEGIN IMMEDIATE TRANSACTION but when I do the same for iOS it appears to not block and attempts to open a transaction inside a transaction.

Was SQLite supposed to block access to transactions or is it the responsibility of the developer using SQLite using something like a semaphore.

I tried to prove this hypothesis by using the async-mutex package to wrap my transactions like this note I only do this operation on iOS. I didn't need to do this on Android.

  /**
   * Creates a transaction and executes a callback passing in the transaction wrapped with an async API
   * @param callback callback function that would get a transaction that provides async capability.  The return value of the callback will be the return value of this method.
   */
  async txn<T>(callback: AsyncTransactionCallback<T>): Promise<T> {
    const fn = async () => {
      try {
        await this.executeSqlAsync('begin immediate transaction');
        const tx = new AsyncSQLTransaction(this);
        const rs = await callback(tx);
        await this.executeSqlAsync('commit');
        return rs;
      } catch (error) {
        await this.executeSqlAsync('rollback');
        throw error;
      }
    };
    if (Platform.OS === 'ios') {
      return this.mutex.runExclusive(fn);
    } else {
      return fn();
    }
  }
Archimedes Trajano
  • 28,830
  • 14
  • 134
  • 195

0 Answers0