如何知道从本地存储或服务器存储中检索数据? [英] How to know if data are retrieved from local storage or server storage?
问题描述
在我的应用程序启动时,我启用了数据持久性:
FirebaseDatabase.getInstance()。setPersistenceEnabled(true);
当我的设备处于离线状态时,firebase会从本地存储中检索数据。
有没有办法知道从本地存储或服务器存储中检索数据? (没有测试我的网络状态)
$ b $ pre $ mDatabase.child(users)。child(userId).addValueEventListener(new ValueEventListener ){
@Override
public void onDataChange(DataSnapshot dataSnapshot){
//知道是否从本地存储或服务器存储中检索数据
}
@Override
public void onCancelled(DatabaseError databaseError){
}
});
谢谢。
目前无法知道Firebase数据库是否提供来自本地缓存或来自服务器的数据。如果你附加了一个监听器,就像你上面所说的那样:除非你没有连接,否则数据通常与服务器保持同步(你可以通过侦听来检测 - 你已经可以检测到 - .info / connected
)。
通常情况下,最好使用不需要数据新鲜度的数据库。因此,将数据视为您的应用程序知道的最新数据。
At the launch of my application I enabled the data persistence :
FirebaseDatabase.getInstance().setPersistenceEnabled(true);
When my device is offline for example, firebase retrieve data from local storage.
Is there a way to know if data are retrieved from local storage or server storage ? (Without test my network state)
mDatabase.child("users").child(userId).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// Know if data are retrieved from local storage or server storage
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
Thank you.
There is currently no way to know if the Firebase Database delivers data that comes from the local cache or from the server. If you attach a listener, like you have above: the data will typically be in sync with the server, unless you're not connected (a case you can detect by listening for - as you already - .info/connected
).
It is typically best to work with the database in a way that doesn't require knowledge on data freshness. So think of the data as "the freshest data that your app knows off".
这篇关于如何知道从本地存储或服务器存储中检索数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!