如何使用 Flutter 减少对 Firestore 的读取?
How can I reduce my reads on Firestore with Flutter?
我知道可以使用命令 GetOptions 来减少不必要的读取(来源:Source.cache)。我的问题是,我如何 运行 我的 Firestore 查询首先通过缓存获取,如果那里什么都没有,即缓存是空的,通过服务器?
我的代码将类似于...
_getLikes() {
User _user = _firebaseAuth.currentUser;
FirebaseFirestore.instance.collection('Users')
.doc(_user.uid)
.collection("Likes")
.where('liked', isEqualTo: true)
.snapshots()
.listen((_querySnapshot) {
//likedList.clear();
if (_querySnapshot.docs.isNotEmpty) {
_querySnapshot.docs.forEach((_likedDocumentSnapshot) async {
await FirebaseFirestore.instance.collection('Users')
.doc(_likedDocumentSnapshot.data()['likedId'])
.get()
.then((_usersDocumentSnapshot) {
if (_usersDocumentSnapshot.exists) {
User _tempUser =
User.fromDocument(_usersDocumentSnapshot);
liked.add(_tempUser);
if (mounted) setState(() {});
}
});
});
}
});
}
用户是我的class,我在其中管理服务器的内容,例如用户的姓名和年龄。
我只需要知道如何使用它
GetOptions(source: Source.cache)
里面
get()
如果缓存为空
GetOptions(source: Source.server)
首先我的函数 getLikes() 应该查看数据是否在缓存中并从那里加载它。如果它不在缓存中,那么它应该从服务器加载数据。
非常感谢您的帮助,在 Google 上找不到任何内容。
First my function getLikes() should look if the data is inside the cache and load it from there. If it is not inside the cache, than it should load the data from server.
您在这里描述的完全是 snapshots()
的工作方式。您是否考虑过使用它?
您可以通过两种方法实现此目的:
- 一个带有源选项。使用源选项,get() 调用
DocumentReference and Query。通过提供来源
值,这些方法可以配置为仅从
服务器,仅从本地缓存,或尝试获取结果
从服务器返回到缓存(这是默认设置)。
这是一个代码示例,展示了如何使用 source options 来了解数据是从缓存、服务器还是默认获取的。
您也可以编写代码先查询缓存,如果不存在则回退到服务器,如下所示:
let snapshot = await documentRef.get({ source: 'cache' }
if (!snapshot.exists) {
snapshot = await documentRef.get({ source: 'server' })
}
这可能是节省读取操作成本的好方法,但如果 source : cache 每次都为 true,应用程序不会从服务器读取文档,而只会从缓存中读取文档,它可能永远看不到未来文档数据的更改。因此,第二个选项 - SnapshotMetadata 更合适。
- 其次,在您的 SnapshotMetadata 中使用 fromCache 属性
快照事件。如果 fromCache 为真,则数据来自缓存
并且可能没有 changes/updates。如果 fromCache 为假,则
数据完整且与服务器上的最新更新一致。
这是一个代码示例,展示了如何使用 SnapshotMetadata changes 来了解数据是从缓存还是服务器中获取的。
我知道可以使用命令 GetOptions 来减少不必要的读取(来源:Source.cache)。我的问题是,我如何 运行 我的 Firestore 查询首先通过缓存获取,如果那里什么都没有,即缓存是空的,通过服务器?
我的代码将类似于...
_getLikes() {
User _user = _firebaseAuth.currentUser;
FirebaseFirestore.instance.collection('Users')
.doc(_user.uid)
.collection("Likes")
.where('liked', isEqualTo: true)
.snapshots()
.listen((_querySnapshot) {
//likedList.clear();
if (_querySnapshot.docs.isNotEmpty) {
_querySnapshot.docs.forEach((_likedDocumentSnapshot) async {
await FirebaseFirestore.instance.collection('Users')
.doc(_likedDocumentSnapshot.data()['likedId'])
.get()
.then((_usersDocumentSnapshot) {
if (_usersDocumentSnapshot.exists) {
User _tempUser =
User.fromDocument(_usersDocumentSnapshot);
liked.add(_tempUser);
if (mounted) setState(() {});
}
});
});
}
});
}
用户是我的class,我在其中管理服务器的内容,例如用户的姓名和年龄。
我只需要知道如何使用它
GetOptions(source: Source.cache)
里面
get()
如果缓存为空
GetOptions(source: Source.server)
首先我的函数 getLikes() 应该查看数据是否在缓存中并从那里加载它。如果它不在缓存中,那么它应该从服务器加载数据。
非常感谢您的帮助,在 Google 上找不到任何内容。
First my function getLikes() should look if the data is inside the cache and load it from there. If it is not inside the cache, than it should load the data from server.
您在这里描述的完全是 snapshots()
的工作方式。您是否考虑过使用它?
您可以通过两种方法实现此目的:
- 一个带有源选项。使用源选项,get() 调用 DocumentReference and Query。通过提供来源 值,这些方法可以配置为仅从 服务器,仅从本地缓存,或尝试获取结果 从服务器返回到缓存(这是默认设置)。
这是一个代码示例,展示了如何使用 source options 来了解数据是从缓存、服务器还是默认获取的。 您也可以编写代码先查询缓存,如果不存在则回退到服务器,如下所示:
let snapshot = await documentRef.get({ source: 'cache' }
if (!snapshot.exists) {
snapshot = await documentRef.get({ source: 'server' })
}
这可能是节省读取操作成本的好方法,但如果 source : cache 每次都为 true,应用程序不会从服务器读取文档,而只会从缓存中读取文档,它可能永远看不到未来文档数据的更改。因此,第二个选项 - SnapshotMetadata 更合适。
- 其次,在您的 SnapshotMetadata 中使用 fromCache 属性 快照事件。如果 fromCache 为真,则数据来自缓存 并且可能没有 changes/updates。如果 fromCache 为假,则 数据完整且与服务器上的最新更新一致。
这是一个代码示例,展示了如何使用 SnapshotMetadata changes 来了解数据是从缓存还是服务器中获取的。