Android(Java)SharedPrefs数据通过Firebase同步 – 仅进行一次请求而不是一堆

huangapple 未分类评论42阅读模式
英文:

Android (Java) SharedPrefs data sync via Firebase - make one request instead of a bunch

问题

我正在尝试寻找同步SharedPrefs数据的最佳方法,但目前还没有想法。

我试图使用自己的方式,它实际上是有效的。在Prefs中有几个文件:“Sets”文件包含一些数据以及关于其他Prefs名称的信息。这种方式首先应该将数据从“Sets”放入,然后再将数据从其子元素放入。

这种方式会向Firebase发送过多的请求。但Firebase对免费请求有一个每月的限制。因此,重要的是要进行一次大的请求,而不是一堆请求。

SharedPreferences Sets = getSharedPreferences("Sets", MODE_PRIVATE);

FirebaseFirestore db = FirebaseFirestore.getInstance();

Map<String, Object> mainSPRowData = (Map<String, Object>) Sets.getAll();

Map<String, Object> mainSP = new HashMap<>();
mainSP.put("ACTIVE_USER", mainSPRowData.get("ACTIVE_USER"));
mainSP.put("USERS_COUNT", mainSPRowData.get("USERS_COUNT"));

DocumentReference documentReference = db.collection("users").document(mAuth.getCurrentUser().getEmail()).collection("Sets").document("MainSet");

documentReference.set(mainSP);
for (String row : (HashSet<String>) mainSPRowData.get("USERS")) {
    SharedPreferences sp = getSharedPreferences(row, MODE_PRIVATE);
    documentReference.getParent().document(row).set(sp);
}

我们应该如何做?

我的Firestore模式:

Firestore-root
    |
    --- users(集合)
    |     |
    |     --- 邮件:mymail@google.com(文档)
    |            | 
    |            --- Sets(集合)
    |                  |
    |                  --- MainSet
    |                        |
    |                        --- ACTIVE_USER: "3"
    |                        --- USERS_COUNT: "21"
    |                  --- user1
    |                  --- user2
    |                  ...
    |                  --- usern

(代码部分已翻译,其余部分保持原样。)

英文:

I'm trying to find the best way to sync the data from SharedPrefs, but there are no ideas.

I'm trying to use own way and it actually works. I have few files in Prefs: "Sets" file that contains some data and info about the names of another Prefs. This way I firstly should put data from "Sets" and then put data from his child elements.

This way we have too many requests to Firebase. But Firebase has a monthly limit of free requests. This way it is important to make one big request instead of a bunch of requests.

SharedPreferences Sets = getSharedPreferences(&quot;Sets&quot;, MODE_PRIVATE);

FirebaseFirestore db = FirebaseFirestore.getInstance();

Map&lt;String, Object&gt; mainSPRowData = (Map&lt;String, Object&gt;) Sets.getAll();

Map&lt;String, Object&gt; mainSP = new HashMap&lt;&gt;();
mainSP.put(&quot;ACTIVE_USER&quot;, mainSPRowData.get(&quot;ACTIVE_USER&quot;));
mainSP.put(&quot;USERS_COUNT&quot;, mainSPRowData.get(&quot;USERS_COUNT&quot;));


DocumentReference documentReference = db.collection(&quot;users&quot;).document(mAuth.getCurrentUser().getEmail()).collection(&quot;Sets&quot;).document(&quot;MainSet&quot;);

documentReference.set(mainSP);
for (String row : (HashSet&lt;String&gt;)mainSPRowData.get(&quot;USERS&quot;)) {
    SharedPreferences sp = getSharedPreferences(row, MODE_PRIVATE);
    documentReference.getParent().document(row).set(sp);
}

How can we?

My Firestore schema:

Firestore-root
    |
    --- users (collections)
    |     |
    |     --- email: mymail@google.com(document)
    |            | 
    |            --- Sets(colletion)
    |                  |
    |                  --- MainSet
    |                        |
    |                        ---ACTIVE_USER: &quot;3&quot;
    |                        ---USERS_COUNT: &quot;21&quot;
    |                  --- user1
    |                  --- user2
    |                  ...
    |                  --- usern

huangapple
  • 本文由 发表于 2020年4月11日 01:31:49
  • 转载请务必保留本文链接:https://java.coder-hub.com/61145546.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定