0

I am moving from sql to nosql database,as a part of it I am working on a flutter project where I am implementing a sequence like structure as below.

  static Future<int> get nextId async {
    return database.get().then((snapshot) {
      return snapshot.value;
    }).then((value) {
      database.set(value + 1);
      return value + 1;
    });
  }

It works, each time i am using nextID it increments in the database. My question is that is it possible when two different users happens to get the same value on invoking nextId.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • The Firebase Realtime Database and Cloud Firestore are two separate databases. Please only mark your question with the relevant tag, not with both. – Frank van Puffelen Nov 18 '21 at 18:41

1 Answers1

0

If you want to prevent multiple users from modifying the same value in conflicting ways, you'll want to use a transaction.

In this case, since you seem to be simply incrementing a value, you may also be helped by using the atomic increment operation which is significantly faster as I showed in: How quickly can you atomically increment a value on the Firebase Realtime Database?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807