react-native-firebase/docs/modules/transactions.md

1.9 KiB

Transactions

!> Transactions is currently an experimental feature in RNFirebase. Whilst it does work there may still be some issues with it, especially around offline connectivity handling. Please report any issues in the usual manner.

?> For help on how to use firebase transactions please see the Firebase Transaction Documentation.

Android Implementation

The android implementation makes use of Condition and ReentrantLock locks to handle transactions across the React Native Bridge.

iOS Implementation

The iOS implementation makes use of GCD (Grand Central Dispatch) to handle transactions across the React Native Bridge without blocking the application thread. Check out this post for some 'light' reading about it.

!> Transactions that receive no response from react native's JS thread within 30 seconds are automatically aborted - this value is currently not configurable - PR welcome.

Example

const ref = firebase.database().ref('user/posts');

ref.transaction((posts) => {
  return (posts || 0) + 1;
}, (error, committed, snapshot) => {
  if (error) {
    console.log('Something went wrong', error);
  } else if (!committed) {
    console.log('Aborted'); // Returning undefined will trigger this
  } else {
    console.log('User posts incremented by 1');
  }

  console.log('User posts is now: ', snapshot.val());
});