Firestore快照侦听器的生命周期与定价之间有什么关系? [英] What relationship between firestore snapshotlistener's lifecycle and pricing?

查看:93
本文介绍了Firestore快照侦听器的生命周期与定价之间有什么关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的活动中,我有一个字符串列表,这些字符串代表要将快照侦听器附加到的Firestore文档.我使用Acivity-ModelView-储存库结构.在活动的onCreate中,我向ViewModelProvider请求适当的ViewModel.在ViewModel构造函数中,我进行调用以获取存储库(按照带有视图的Android房间"教程).我的存储库负责附加firestore侦听器,并将在线数据同步到本地数据库(android会议室)中

In my activities, I have a list of strings which represent firestore documents to which I want to attach snapshot listeners. I use Acivity - ModelView - Repository structure. In the acitivity's onCreate, I ask ViewModelProvider for the appropriate ViewModel. In the ViewModel constructor, I make a call to get a repository (as per the "Android room with a view" tutorial"). My repositories are in charge of attaching firestore listeners and sync the online data into my local DB (android room).

我以前经常与这些监听器发生内存泄漏,即每次更改Firestore文档时,我的存储库都试图将其中的两个,三个,四个..副本下载到本地数据库中!我通过从活动的onDestroy一直到存储库中删除侦听器的方式来解决了该问题.

I used to have memory leaks with those listeners, i.e. every time a firestore document changed, my repository was trying to download two, three, four..copies of it into the local DB! I solved that problem by making a call from my activities' onDestroy all the way up to the repository to remove the listeners.

我的问题是关于该解决方案的价格.我在FireBase网站上读到,快照侦听器每次启动时,至少都会算作一次文档读取",即使从未对文档进行任何更改.基本上,每次用户在我的应用中切换活动时,我都会删除并重新绑定十几个监听器(完全相同的文档).这是否意味着即使30分钟的限制还没有结束,我也要为每项活动的更改支付一份阅读文件?

My question is about the pricing of this solution. I read on the FireBase website that a snapshot listener will count at least as one "document read" everytime it is launched, even if no changes to the document are ever made. Basically, I am removing and re-attaching a dozen or so listeners (to the exact same documents) every time a user switches activities in my app. Does this mean I am paying a document read for every one of those activity changes even if the 30-minute limit isn't up yet?

活动

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mMessageViewModel = new ViewModelProvider(this).get(MessageViewModel.class);
    // ....
}

@Override
public void onDestroy(){
    mMessageViewModel.removeListeners();
    super.onDestroy();
}

ViewModel

public MessageViewModel (Application application) {
    super(application);
    mRepository = new MessageRepository(application);
}
public void removeListeners(){
    mRepository.removeListeners();
}
// ...

存储库

private List<ListenerRegistration> my_listeners;
private List<String> my_list;
MessageRepository(Application application) {
    MessageRoomDatabase db = MessageRoomDatabase.getDatabase(application);
    mMessageDao = db.messageDao();
    firedb = FirebaseFirestore.getInstance();
    attachListeners();
}
public void attachListeners(){
    for(String item : my_list){
        colRef = firedb.collection("items").document(item).collection("sub-items");
        ListenerRegistration my_listener_registration = colRef
            .addSnapshotListener(myListener);
        my_listeners.add(my_listener_registration);
    }
}
public void removeListeners(){
    for(ListenerRegistration my_listener : my_listeners){
        my_listener.remove();
    }
}
// ...

推荐答案

每次附加侦听器时,Firestore客户端都必须连接到服务器,以检查该侦听器观察到的文档是否已被修改.由于服务器必须为此阅读文档,因此您确实需要为您观察到的每个文档的阅读文档付费.

Every time you attach a listener, the Firestore client has to connect to the server to check if the documents observed by that listener have been modified. Since the server has to read the document for that, you will indeed be charged for a document read for each document that you observe.

如果您不想这样做,可以考虑通过

If you don't want this, you can consider telling the client to read from the cache by specifying that in the source options.

DocumentReference docRef = db.collection("cities").document("SF");

// Source can be CACHE, SERVER, or DEFAULT.
Source source = Source.CACHE;

// Get the document, forcing the SDK to use the offline cache
docRef.get(source).addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
   ...

由于这是从本地缓存中读取的内容,因此您无需为服务器上的读取操作付费,但这当然意味着您可能在提供过时的数据.

Since this reads from the local cache, you won't be charged for a read on the server, but of course that means you may be serving stale data.

这篇关于Firestore快照侦听器的生命周期与定价之间有什么关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆