Angular Firestore查询中的get()和valueChanges()有什么区别? [英] What is the difference between get() and valueChanges() in Angular Firestore query?

查看:77
本文介绍了Angular Firestore查询中的get()和valueChanges()有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题所述,我想问一下在Angular Firestore中执行查询时get()和valueChanges()有什么区别.

As the title says I would like to ask what is the difference (if there is any) between get() and valueChanges() when performing a query in Angular Firestore.

两者之间在读取/成本方面是否还存在任何优点/缺点?

Are there also any advantages/disadvantages between the two maybe regarding the reads/costs?

推荐答案

valueChangesget()之间的主要区别在于,使用get(),您只能一次获得数据,而valueChanges(和snapshotChanges)将在链接到您正在收听的该文档/集合的数据库中发生任何更改时自动触发.

The main difference between valueChanges and get(), is that with get(), you get the data only once, whereas valueChanges (and snapshotChanges) is automatically fired whenever something changes in the database linked to that document/collection that you are listening to.

后者是Firebase实时数据库的优点,因为您不需要轮询或其他任何方法即可获取最新数据,因此Firebase会处理所有这些问题!

The latter is the beauty of firebase realtime database, since you don't need to poll or anything else to get the latest data, firebase takes care of all of that!

我认为get()在例如更新集合中的文档,然后在更新后立即希望对该文档执行某项操作,并且仅获取一次,例如:

In my opinion get() is useful to use when you for example update a document in a collection, and then instantly want to do something with that document after the update, and only fetch that once, like:

const docRef= this.afs.collection(colId).doc(docId).set(...)

docRef.get().pipe(
  map(doc => doc.data())
)
.subscribe(data => {
   // do stuff with document
})

当然,您可以使用例如valueChanges来调用文档并附加一个pipe(take(1)),但是在这种情况下get()非常方便.

Of course you could call the document with for example valueChanges and attach a pipe(take(1)), but get() is pretty handy in this case.

这篇关于Angular Firestore查询中的get()和valueChanges()有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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