Flutter Firebase全局自动增量值并在文档字段中检索 [英] Flutter Firebase global Auto Incremental value and retrieving in document field dart

查看:104
本文介绍了Flutter Firebase全局自动增量值并在文档字段中检索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚接触Firebase Flutter。我正在开发一个移动应用程序以共享书籍。

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

在Firebase firestore中,

In firebase firestore,


  • 我有用户'包含所有具有唯一ID的用户数据的收藏集

  • 我有一个'books'包含所有具有自动创建的唯一ID的图书数据

  • 我也有单个文档的全局集合,其中一个整数字段称为 bookcount。

  • 用户可以拥有很多书。

  • 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.

现在我想为书创建另一个唯一的id字段。想法是拥有简单的整数id。
这样做的一种方法是获取书籍清单并找到长度(计数),并在创建新记录时加1。我已经排除了这种方法,就好像许多用户同时使用时一样,我认为这会导致重复的ID。

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.

因此,我创建了另一个具有单个文档和字段名称记帐簿的全局集合。哪个在图书收藏中保留图书数量(计数)。因此,每次将书添加到馆藏时,都会增加书数并将此值用作书的简单唯一ID。由于用户可以在保存之前丢弃该条目,所以该帐面数量可能并不代表实际的书籍,因为我只需要一个简单的唯一ID,就可以了。

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. 我们可以一次完成吗?

如果我尝试在返回值时非常快连续地连续调用此方法,则可能会跳过几个数字。我已从一个按钮调用此按钮,并尝试尽快按下。我认为计数器增加,但是它几次返回相同的
。然后再次按下时跳过一些。例如1,2,3,4,8,8,8,8,9,10,...因此,在计数器4处,我尝试多次按下按钮。我不知道当多个用户同时添加多本书时会如何表现。

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. 如何解决此问题?

请帮助,谢谢。

推荐答案

我认为问题出在因为 await globalData.document('SomeHardcodedID')。updateData 不会产生返回值(void),所以一旦触发下一个调用也将执行,这没关系,这没关系对于大多数情况。
但是,如果bookId在很短的时间内(毫秒)被调用了几次,则在 FieldValue.increment(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.

这篇关于Flutter Firebase全局自动增量值并在文档字段中检索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆