Flutter Firebase global Auto Incremental value and retrieving in document field dart

[亡魂溺海] 提交于 2021-02-11 02:00:31

问题


I'm quite new to Firebase Flutter. I'm developing a mobile application to share books among others.

In firebase firestore,

  • I have 'users' collections which contain all the user data with unique id
  • I have 'books' collection which contain all the book data with unique id created automatically
  • Also I have 'global' collection with single document with one integer field called 'bookcount'.
  • Users can can have many books.

Now I want to create a another unique id field for book. idea is to have simple integer id. One way of doing this is get list of books and find the length (count) and add 1 when creating a new record. I have ruled out this method as if many users using simultaneously, I think this can lead to duplicate ids.

So I have created a another collection global with single document and field name bookcount. Which hold number of books (rough count) on books collection. So idea is each time when adding a book to a collection increase bookcount and use this value as simple unique id for a book. This bookcount may not represent actual books as user can discard the book entry before saving it, which is okay as I only need a simple unique id.

class DatabaseService {
      ...
      ...
  
  //final CollectionReference bookCollection = Firestore.instance.collection('users');
  //final CollectionReference bookCollection = Firestore.instance.collection('books');
  final CollectionReference globalData = Firestore.instance.collection('global');
      ...
      ... 
Future<String> bookId() async 
    {
        String uniquebookid =  await globalData.document('SomeHardcodedID').updateData(
        {
        'bookcount': FieldValue.increment(1)
        }).then((voidvalue) async 
        {
            String cid = await globalData.getDocuments().then((bookvalue) => bookvalue.documents.single.data['bookcount'].toString());
            return cid;
        });
    return uniquebookid;
    }//future bookId
      ...
      ...
}//class

Now this works. well somewhat, Can we do this better? In here there are two parts, first increment the value bookcount, and then retrieve it.

  1. Can we do this in one go?

If I try to call this method consecutively really fast when returning a value it might skip few numbers. I have call this from a button and try to press as fast I could. I think counter increase but it return same number few times. and then skip some when press again. for example 1,2,3,4,8,8,8,8,9,10,... So at counter 4 I try to press the button multiple times. I wonder how this will behave when multiple users adding multiple books at the same time.

  1. How Can I fix this?

Please Help, Thanks.


回答1:


I think the problem was since await globalData.document('SomeHardcodedID').updateData is not producing a return value (void), as soon as this fired next call also execute which is okay, which okay for most scenarios. However if bookId called few times within very short period (milliseconds) this produce number before FieldValue.increment(1) process.



来源:https://stackoverflow.com/questions/63368349/flutter-firebase-global-auto-incremental-value-and-retrieving-in-document-field

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!