Home > Enterprise >  How to get a "Sequence" or counters like structure in firebase realtime database
How to get a "Sequence" or counters like structure in firebase realtime database

Time:11-19

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.

CodePudding user response:

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?

  • Related