在Firestore中使用日历读取相关数据

时间:2019-03-25 13:04:21

标签: android firebase google-cloud-firestore

在我的应用程序中,根据日期需要一些固定数据。

我的意图是

  • 避免每年都要更新这些数据,因为它们总是一样的。

  • 根据与之对应的日期放置每条信息。

根据该问题How to organize related temporal and fixed data in Firestore?的讨论,我决定将固定数据存储在一个集合中,然后创建一个calendar集合,该集合将在给定日期的情况下知道什么内容对应。

同时,我希望避免消耗最少的东西:根据我的模型,将首先读取calendar,并根据给定日期查找与该日期对应的内容。然后将根据该参考文献搜索内容。

  • 将有两个读数,一个是日历,另一个是具有固定内容的集合。

  • calendar集合以及oficiolaudes集合将由应用程序同步并缓存。

固定内容

Firestore-root
   |
   --- liturgia (collection)
   |     |
   |     --- lh (documents)
   |          |
   |          --- oficio (collection)
   |                |
   |                --- 03070101 (documents)
   |                        |
   |                        --- himno: "Today himno for today oficio"
   |                        |
   |                        --- read: "Today read for today oficio"
   |
   |                --- 03070102 (documents)
   |                        |
   |                        --- himno: "Today himno for today oficio"
   |                        |
   |                        --- read: "Today read for today oficio"
   |
   |                --- 09010325 (documents)
   |                        |
   |                        --- himno: "Today himno for today oficio"
   |                        |
   |                        --- read: "Today read for today oficio"
   |
   |          --- laudes (collection)
   |                |
   |                --- 03000101 (documents)
   |                        |
   |                        --- himno: "Today himno for today laudes"
   |                        |
   |                        --- read: "Today read for today laudes"
   |
   |                --- 03000102 (documents)
   |                        |
   |                        --- himno: "Today himno for today laudes"
   |                        |
   |                        --- read: "Today read for today laudes"
   |
   |                --- 09010325 (documents)
   |                        |
   |                        --- himno: "Today himno for today laudes"
   |                        |
   |                        --- read: "Today read for today laudes"

可更新的内容

Firestore-root
   |
   --- calendar (collection)
   |     |
   |     --- 20190325 (documents)
   |          |
   |          --- oficio: "09010325"
   |          |
   |          --- laudes: "09010325"
   |          |
   |          --- tercia: "03000301"   
   |
   |
   |      --- 20190326 (documents)
   |          |
   |          --- oficio: "03070102"
   |          |
   |          --- laudes: "03000102"
   |          |
   |          --- tercia: "03000102"

代码

今天检索oficio文档的代码,

   |                --- 09010325 (documents)
   |                        |
   |                        --- himno: "Today himno for today oficio"
   |                        |
   |                        --- read: "Today read for today oficio"

String todayDate="20190325"; //getted automatically
    final DocumentReference docRef = db.collection("calendar").document(todayDate);
    docRef.addSnapshotListener(new EventListener<DocumentSnapshot>() {
        @Override
        public void onEvent(@Nullable DocumentSnapshot snapshot,
                            @Nullable FirebaseFirestoreException e) {
            if (e != null) {
                Log.w(TAG, "Listen failed.", e);
                //launchVolley();
                return;
            }

            if (snapshot != null && snapshot.exists()) {
                String contentPath=snapshot.getString("oficio");
                DocumentReference docRef = db.collection("liturgia").document("lh").collection("oficio")
                        .document(contentPath);
                docRef.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
                    @Override
                    public void onSuccess(DocumentSnapshot documentSnapshot) {
                        mBreviario = documentSnapshot.toObject(Breviario.class);
                        Log.w(TAG, documentSnapshot.toString());
                        showData();
                    }
                });
            } else {
                //launchVolley();
            }
        }
    });

关于这种情况,我有两个疑问:

  1. 我正在进行两次咨询,一次是根据今天的日期获取参考,另一次是根据获取的参考获取固定内容。由于我计划缓存日历,因此当应用程序从缓存中读取日历时,还是仅当它在Firebase中读取日历时,我才需要付费?

  2. 因为固定的集合也会被缓存,并且会包含很多信息:每组至少365个文档(oficiolaudes等)。就日历而言,缓存将如何工作。我想确定calendar缓存的优先级,并根据缓存限制添加其他内容。

0 个答案:

没有答案