【发布时间】:2020-07-03 14:08:36
【问题描述】:
我正在构建一个用户登录到应用程序时的位置,它会显示所有用户的信息,这些信息也在应用程序上注册。使用我的 init fetch 方法获取所有信息,它使用大量读取,在几天的测试中达到 1.3k 读取,而只有 4 个用户注册。这是异常高的。
我的 fetch 方法从 firestore 中获取用户信息
void loadUserProfiles() async {
var firebaseUser = await FirebaseAuth.instance.currentUser();
List<String> tempUsers = List<String>();
List<String> tempNames = List<String>();
List<String> tempImages = List<String>();
imageUrl.clear();
names.clear();
userId.clear();
tempUsers.clear();
tempNames.clear();
tempImages.clear();
setState(() {
isLoading = true; //Data is loading
});
// Adds all the values of each user from firestore to their list to compare
await firestoreInstance
.collection("users")
.getDocuments()
.then((QuerySnapshot snapshot) async {
snapshot.documents.forEach((f) async {
if (f.documentID != firebaseUser.uid) {
tempUsers.add(f.data['userid']);
tempNames.add(f.data['name']);
tempImages.add(f.data['images'][0]);
}
});
});
// Adds user to list to load to user cards if doesnt exist in liked users firestore
for (int i = 0; i < tempUsers.length; i++) {
await firestoreInstance
.collection("users")
.document(firebaseUser.uid)
.collection("liked_users")
.document(tempUsers[i])
.get()
.then((value) async {
if (!value.exists) {
userId.add(tempUsers[i]);
names.add(tempNames[i]);
imageUrl.add(tempImages[i]);
}
});
}
setState(() {
isLoading = false; //Data has loaded
});
}
我这样做的方式是它获取所有数据并将它们存储到三个单独的临时列表中。然后使用这些列表,我将再次阅读 firestore 以比较当前用户喜欢的用户 id 是否存在于集合中。
它也变得更糟,因为我使用底部导航栏,每当点击该页面时,它都会再次启动我的加载方法,该方法使用更多读取。
【问题讨论】:
-
您如何确定所有读取都来自这个单一操作?似乎不太可能在每个级别使用深度 2 和最大实体 4 的嵌套集合,它可以乘以多达 1300 个读取
-
很抱歉,我应该说得更清楚。我不是说它来自一个操作,我已经测试了几天,但我确信它不应该有 4 个用户那么高
-
下一次,尝试添加更多详细信息,例如您已测试的天数,甚至粗略估计您尝试运行此功能的次数
标签: firebase flutter dart google-cloud-firestore backend