Firebase在Android应用空闲后无法获取新数据。

huangapple go评论77阅读模式
英文:

Firebase not getting new Data, after Android app was idle

问题

Info:

我正在开发一个应用程序,多个用户需要共同完成一个项目。有一些任务需要完成。

Problem:

当用户关闭屏幕,然后在大约5分钟后再次打开屏幕(或者在主屏幕或其他应用程序停留约5分钟),Firebase 需要大约一分钟将新数据传递到应用程序中(我已关闭离线存储)。这让用户感到沮丧,因为数据需要始终保持最新,以便他们可以使用更新后的任务。

EDIT这在所有实现中都普遍存在,就像下面的示例一样

Question:

为什么会出现这种情况?
我该如何解决这个问题??

EDIT示例:

final FirebaseFirestore db = FirebaseFirestore.getInstance();
DocumentReference doc = db.collection("user").document(email).collection("dates").document(date);
doc.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
        @Override
        public void onComplete(@NonNull Task<DocumentSnapshot> task) {
            if (task.isSuccessful()) {
                DocumentSnapshot document = task.getResult();
                if (document.exists()) {

                    HashMap CodeMap = (HashMap) document.getData();
                    Set keySet = CodeMap.keySet();
                    Object[] key = keySet.toArray();
                    for (int i = 0; i < keySet.size(); i++) {

                        if (!key[i].equals("clockIn") && !key[i].equals("clockOut") && !key[i].equals("completeTime") && !key[i].equals("clockPauseFrom") && !key[i].equals("clockPauseTo")) {

                            userWorkList.add(new User_Work_View_Row(key[i].toString(), email, date, i));
                            System.out.println(key[i] + " FOUND");
                        }
                    }
                }else{
                    Log.d(TAG, "get failed with ", task.getException());
                    getActivity().finish();
                }
            }
        }

});

解决方案:*(有些类似)

我已经实施了下面答案中的解决方案,并进行了一些关于电池选项的测试。

我发现禁用电池优化和后台限制(如果有的话)可以解决问题!数据会立即传递过来。我不认为这是一个好的解决方案,但对于我的应用程序有效。

英文:

Info:

I am doing an application where multiple Users need to work on one project. There are a few tasks that need to be completed.

Problem:

When a user turns off his screen, and turns it on around 5 minutes later (Or he went to the Homescreen or any other app for around 5 min ), firebase takes up to a Minute to get the new Data into the App (I have offline storing off btw). It frustrates the Users, because the Data needs to be up to date all the time so they can work with updated Tasks.

EDIT: THIS IS HAPPENING EVERYWHERE IN ALL IMPLEMENTATIONS LIKE THE EXAMPLE BELOW

Question:

Why is it like this?
How can I solve it???

EDIT: Example:

final FirebaseFirestore db = FirebaseFirestore.getInstance();
DocumentReference doc = db.collection(&quot;user&quot;).document(email).collection(&quot;dates&quot;).document(date);
doc.get().addOnCompleteListener(new OnCompleteListener&lt;DocumentSnapshot&gt;() {
        @Override
        public void onComplete(@NonNull Task&lt;DocumentSnapshot&gt; task) {
            if (task.isSuccessful()) {
                DocumentSnapshot document = task.getResult();
                if (document.exists()) {

                    HashMap CodeMap = (HashMap) document.getData();
                    Set keySet = CodeMap.keySet();
                    Object[] key = keySet.toArray();
                    for (int i = 0; i &lt; keySet.size(); i++) {

                        if (!key[i].equals(&quot;clockIn&quot;) &amp;&amp; !key[i].equals(&quot;clockOut&quot;) &amp;&amp; !key[i].equals(&quot;completeTime&quot;) &amp;&amp; !key[i].equals(&quot;clockPauseFrom&quot;) &amp;&amp; !key[i].equals(&quot;clockPauseTo&quot;)) {

                            userWorkList.add(new User_Work_View_Row(key[i].toString(), email, date, i));
                            System.out.println(key[i] + &quot; FOUND&quot;);
                        }
                    }
                }else{
                    Log.d(TAG, &quot;get failed with &quot;, task.getException());
                    getActivity().finish();
                }
            }
        }

                });

SOLUTION:* (Kind of)

I have Implemented the solution of the answer below and did some Testing with the Battery options.

I found out that disabling Battery Optimizations and Background restrictions(if its even there) works! The Data is coming right away. I don't think its a good solution but it works for my App.

答案1

得分: 0

你所观察到的可能是Firestore SDK执行连接重试的频率遵循了指数退避(Exponential Backoff)策略。与在紧密循环中始终立即重试相比,这种退避策略可以节省很多电量。

如果您想强制执行重试,您可以尝试手动调用disableNetwork(),然后再调用enableNetwork(),以使其立即进行重试。

英文:

What you're observing is likely a result of an exponential backoff in the frequency of connection retries performed by the Firestore SDK. This backoff saves much battery compared to always retrying immediately in a tight loop.

If you want to force a retry, then you could try manually calling disableNetwork() followed by enableNetwork() to get it to retry immediately.

huangapple
  • 本文由 发表于 2020年10月12日 02:12:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/64307457.html
匿名

发表评论

匿名网友

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

确定