如何减少Firebase Cloud Firestore上的读取操作?

时间:2019-06-07 12:22:38

标签: firebase flutter google-cloud-firestore instance snapshot

作为一名充满激情的高中老师,我将Firebase Cloud Firestore&Flutter用于我的只读应用程序。我的数据是大约2000个image.png文件,带有一些作为类别和子类别的标签。类别,子类别和图像之间具有顺序。我使用Cloud Firestore的原因是,我可以随时更改(=添加,删除,更新)类别子类别和图像,并且我的用户将能够使用我的最新内容进行更新。

到目前为止,一切正常,我的应用程序可以脱机运行,可以与我的内容同步,但是,如果设备在线,即使db中没有任何更改,Firestore中的所有文档也会每次都被读取。那使我认为自己犯了一个大错误。

这是打开应用程序时发生的代码:

return FutureBuilder<QuerySnapshot>(
      future: _getCategories(),
      builder: (context, catSnapshot) {

            if((!catSnapshot.hasData) && !catSnapshot.hasError) return customProgressIndicator("Categories Synchronizing");

        if(_categories.isNotEmpty) _categories.clear();
        _categories = catSnapshot.data.documents.map<Category>((document) => Category.fromJson(document.data)).toList();

        return FutureBuilder(
          future: _getSubCategories(),
          builder: (context, subSnapshot) {
            if(!subSnapshot.hasData && !subSnapshot.hasError) return customProgressIndicator("Subcategories Synchronizing");

            if(_subcategories.isNotEmpty) _subcategories.clear();
            for(int i=0; i<_categories.length; i++) {
              String catName = _categories[i].name;
              List<Subcategory> subcategoriesI = subSnapshot.data.documents.where((document) => document.data["catName"]==catName).map<Subcategory>((document) => Subcategory.fromJson(document.data)).toList();
              _subcategories[catName] = subcategoriesI;
            }

            return RefreshIndicator(
              onRefresh: _onRefresh,
              child: _categories.length==0 ? whenNoData() : ListView.builder(
                itemBuilder: (context, i) => _createCategoryItem(i),
                itemCount: _categories.length,
                padding: EdgeInsets.all(8.0),
              ),         );       },      );    },   )

这是我的主页:

class _CategoryListPageState  extends State<CategoryListPage> {
  List<Category> _categories = [];
  Map<String, List<Subcategory>> _subcategories = {};

 Future<QuerySnapshot> _getCategories() async{

    var conn=await UserHasConnection();
    print("connection: " + conn.toString());
    Future<QuerySnapshot> snapshots;

      if (widget.isUserPro) {
        snapshots = Firestore.instance.collection("categories").where("isPro", isEqualTo: true).orderBy("showOrder").getDocuments();
        snapshots.timeout(Duration(seconds: globalTimeOut), onTimeout: () {print("Timeout Category");});
        return snapshots;
      }   else {
        snapshots = Firestore.instance.collection("categories").orderBy("showOrder").getDocuments();
        snapshots.timeout(Duration(seconds: globalTimeOut), onTimeout: () {print("Timeout Category");});
        return snapshots;
      }
  }

  _getSubCategories() async{
    Future<QuerySnapshot> snapshots;
      if(widget.isUserPro){
    snapshots=Firestore.instance.collection("subcategories").where("isPro", isEqualTo: true).orderBy("showOrder").getDocuments();
        snapshots.timeout(Duration(seconds: globalTimeOut), onTimeout: () {
          print("Timeout Subcategory");
        });
        return snapshots;
      }  else  {  snapshots=Firestore.instance.collection("subcategories").orderBy("showOrder").getDocuments();
        snapshots.timeout(Duration(seconds: globalTimeOut), onTimeout: () {
          print("Timeout Subcategory");
        });
        return snapshots;
      }
  }

这些是我的Firestore文档集合:

categories
  isPro: boolean (there aid free and paid-proUser's)
  name: string, name of the category
  scCount: int, number of subcategories it has
  showOrder: its order among other categories

subcategories
  catName: name of the category it belongs to
  fcCount: number of image.png it has
  imageBytes: subcategories have an image
  isPro: boolean, subcategories can also be free or paid
  name: subcategories have a name
  showOrder: each subcategory has an order among other subcategories in that category
  title: title of the subcat, similar to name

flashcards
  backBytes: back image base64encoded
  backStamp
  catName: category of this image belongs to
  frontBytes: front image base64encoded
  frontStamp
  isPro: cards are also free or paid (for search function below)
  name
  showOrder: order of this card in the subcategory
  subName: name of the subcategory that this card belongs to

***我有分别用于不同设备屏幕尺寸的flascards,flashcards2x,flashcards3x集合

还有一个搜索功能,该功能通过flashcardContents集合的frontContent属性完成:

flashcardContents
  frontContent
  globalOrder
  isPro
  name
  showOrder
  updateTime
  updateType: (1-adden, 2-updated, 3-deleted)

最后,我的FirebaseService功能:

class FlashcardFirebaseService extends IServiceBase {
  Future<List<Flashcard>> QueryRatedFlashCards(List<Flashcard> _flashcards,
      List<String> flashcardNames, String ratioPostfix, bool isUserPro) async {
    var snapshot;
    flashcardNames.forEach((flashcardName) {
      print("flashcardName: " + flashcardName);

      if (isUserPro) {
        //For pro user
        snapshot = Firestore.instance
            .collection("flashcards${ratioPostfix}")
            .where("name", isEqualTo: flashcardName)
            .snapshots();
      } else {
        snapshot = Firestore.instance
            .collection("flashcards${ratioPostfix}")
            .where("name", isEqualTo: flashcardName)
            .where("isPro", isEqualTo: false)
            .snapshots();
      }

      snapshot.listen((onData) {
        _flashcards.addAll(onData.documents
            .map<Flashcard>((document) => Flashcard.fromJson(document.data))
            .toList());
      });
    });
    return _flashcards;
  }

  static Future<List<Category>> SyncCategories(bool isPro, double lastStamp){
    Firestore.instance
        .collection("categories")
        .getDocuments().then((data){
          return data;
    });
  }

  static Future<List<Category>> SyncSubcategories(bool isPro){
    Firestore.instance
        .collection("subcategories")
        .getDocuments().then((data){
      return data;
    });      }  }


class FlashcardSearchService extends IServiceBase {
  static Future SyncLocalSearchDb() async {
    Query baseQuery;
    AppConfig appConfig;
    appConfig = await AppConfigService.GetAppConfiguration().then((appConfig){
      if (appConfig != null) {
        var lastSyncDate;
        try {
          lastSyncDate = appConfig.fcLastSync;
        } catch (ex) {}

        print("lastSyncDate ---------------------------:"+ (lastSyncDate/1000).toString());

        baseQuery = Firestore.instance
            .collection("flashcardContents")
            .where("updateTime", isGreaterThan: lastSyncDate/1000);
      } else {
        appConfig = null;

        print("appConfig null");

        //Create all card contents
        baseQuery = Firestore.instance.collection("flashcardContents");
        appConfig = new AppConfig();
      }

      baseQuery.getDocuments().then((query) {
        query.documents.forEach((doc) => _syncFlashcardContent(doc));
        if(query.documents.length>0)
          {
            appConfig.fcLastSync = new DateTime.now().millisecondsSinceEpoch.toDouble();
            print("new lastSyncDate ---------------------------:"+ (appConfig.fcLastSync /1000).toString());
            print("SYNC!!! " + appConfig.fcLastSync.toString());
         AppConfigService.UpdateApplicationConfigurationSyncTime(appConfig);

          } else {
              print("CANT SYNC!!! " + appConfig.fcLastSync.toString());
            }
        return appConfig;
      }).timeout(Duration(seconds: globalTimeOut),onTimeout: (){
        print("TimeOutSync");
        return appConfig;
      });
    });

总而言之,我有一个抽认卡image.png集合供我的学生学习。我的内容每周都会有所变化,我希望用户能够跟上我的发展。问题是,使用我的代码,如果设备处于联机状态,即使没有更改,并且即使在几秒钟后关闭应用程序,我也会不断读取所有文档。这对我来说负担不起。

很抱歉,很长一段时间,我希望至少您喜欢该代码,任何想法都是有价值的并且值得赞赏。

我发现我的应用具有如此多的读取计数的主要原因是:“如果监听器断开连接超过30分钟(例如,如果用户离线),那么您将被收取读取费用,就像您发出了全新的查询。” 因此,新的问题是:是否可以更改此侦听器的行为?还是有办法绕过它?我觉得我的应用不太适合Firestore,但到目前为止,这似乎只是个问题!

要总结一下:例如,我需要一个每天工作一次的侦听器,因为我的收藏集可能每天都会更改一次。或者我需要一种绕过此方法的方法。例如,当我的应用程序离线时,侦听器将不起作用。是否有任何选项可以在不禁用Firestore离线持久性的情况下禁用我的应用程序网络连接?谢谢

1 个答案:

答案 0 :(得分:0)

由于我们不了解您应用程序的全部操作(我们没有您的全部源代码,也没有我们提供的服务,所以在Stack Overflow上实际上无法诊断出数据库中所有读取的源)了解用户的行为)。但是,意外读取的一个非常常见的来源来自Firebase控制台本身。当您使用控制台浏览数据库时,为了填充控制台,将花费您一定的时间来阅读。如果您在正在被更改的集合上将控制台窗口保持打开状态,那么在控制台打开时,随着时间的推移,这将进一步累积读取。