使用 AngularFire2 检查 Firebase 数据库中是否存在大量数据
Using AngularFire2 to check if large amount of data exists in Firebase database
最佳实践问题
情况:
我正在使用 Angularfir2 尝试找到最好的方法来检查我在本地存储的项目列表中的哪些项目存在于我的 Firebase 数据库中(如果有的话)。这是考虑到我的 Firebase 数据库中有大量数据(在这个例子中有 100k + 个条目,但可能超过 1M)和大量本地条目(在这个例子中有 200 + 但它可能超过 1000)
下面是 Firebase 数据库的示例:
"project-1234": [
{
"animals" : {
"1" : {
"animal" : "aardvark"
},
...
"100001" : {
"animal" : "zebra"
}
}
}
]
这里是本地存储的 json 数据的示例:
[
{"my_id"=1, "fb_id"=346, "animal"="bear"},
...
{"my_id"=201, "fb_id"=45663, "animal"="water buffalo"}
]
现在我明白了,我可以简单地遍历每个项目,然后进行 Angularfire2 "object" 调用(甚至 http.get)以查看该项目是否存在于数据库中:
this.data = [{...},...,{...}]; // local json data
for (let i in this.data) {
this.fbData.object('animals\' + this.data[i].fb_id).subscribe(dataObj => {
if (dataObj != null){
console.log('There is a : ', this.data[i].animal);
}
});
}
或
this.data = [{...},...,{...}]; // local json data
for (let i in this.data) {
let url = 'https://project-1234.firebaseio.com/animals/' + this.data[i].fb_id + '.json';
this.http.get(url).map(res => res.json()).subscribe((dataObj) => {
if (dataObj != null){
console.log('There is a : ', this.data[i].animal);
}
})
}
问题:
虽然这是我能想到的实现目标的唯一方法,但即使是我的中级理解,这似乎也有点费力,更不用说速度越来越慢,本地保存的记录越多;所以我想知道是否有更好的解决方案或可以在这种情况下使用的最佳实践?
非常感谢任何help/suggestions。
AngularFire2(或 Firebase SDK)实现将比 HTTP 实现更高效,因为请求将被流水线化 - 有关更多详细信息,请参阅 。
要检索指示项目键是否存在的布尔值数组,您可以这样做:
import 'rxjs/add/operator/concatMap';
import 'rxjs/add/operator/first';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/toArray';
Observable.from(items)
.concatMap(item => this.fbData.object(`animals/${item.fb_id}`).first().map(Boolean))
.toArray()
.subscribe((result) => console.log(result));
请注意,first
运算符用于从 AngularFire2 对象可观察对象中检索单个值。如果没有 first
运算符,对象 observables 将无法完成并将继续侦听和发出更改。
这个实现可能是可以接受的。你说"seems a little labor intensive not to mention slow and getting slower",但是你剖析过吗?
在不了解您的应用程序的情况下,很难提供建议。例如,您提到它的"getting slower the more records are held locally",但是一旦您检查了数据库中是否存在一条记录,您是否可以不将其存储在本地?如果你能做到这一点,也许你没有问题?
最佳实践问题
情况:
我正在使用 Angularfir2 尝试找到最好的方法来检查我在本地存储的项目列表中的哪些项目存在于我的 Firebase 数据库中(如果有的话)。这是考虑到我的 Firebase 数据库中有大量数据(在这个例子中有 100k + 个条目,但可能超过 1M)和大量本地条目(在这个例子中有 200 + 但它可能超过 1000)
下面是 Firebase 数据库的示例:
"project-1234": [
{
"animals" : {
"1" : {
"animal" : "aardvark"
},
...
"100001" : {
"animal" : "zebra"
}
}
}
]
这里是本地存储的 json 数据的示例:
[
{"my_id"=1, "fb_id"=346, "animal"="bear"},
...
{"my_id"=201, "fb_id"=45663, "animal"="water buffalo"}
]
现在我明白了,我可以简单地遍历每个项目,然后进行 Angularfire2 "object" 调用(甚至 http.get)以查看该项目是否存在于数据库中:
this.data = [{...},...,{...}]; // local json data
for (let i in this.data) {
this.fbData.object('animals\' + this.data[i].fb_id).subscribe(dataObj => {
if (dataObj != null){
console.log('There is a : ', this.data[i].animal);
}
});
}
或
this.data = [{...},...,{...}]; // local json data
for (let i in this.data) {
let url = 'https://project-1234.firebaseio.com/animals/' + this.data[i].fb_id + '.json';
this.http.get(url).map(res => res.json()).subscribe((dataObj) => {
if (dataObj != null){
console.log('There is a : ', this.data[i].animal);
}
})
}
问题:
虽然这是我能想到的实现目标的唯一方法,但即使是我的中级理解,这似乎也有点费力,更不用说速度越来越慢,本地保存的记录越多;所以我想知道是否有更好的解决方案或可以在这种情况下使用的最佳实践?
非常感谢任何help/suggestions。
AngularFire2(或 Firebase SDK)实现将比 HTTP 实现更高效,因为请求将被流水线化 - 有关更多详细信息,请参阅
要检索指示项目键是否存在的布尔值数组,您可以这样做:
import 'rxjs/add/operator/concatMap';
import 'rxjs/add/operator/first';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/toArray';
Observable.from(items)
.concatMap(item => this.fbData.object(`animals/${item.fb_id}`).first().map(Boolean))
.toArray()
.subscribe((result) => console.log(result));
请注意,first
运算符用于从 AngularFire2 对象可观察对象中检索单个值。如果没有 first
运算符,对象 observables 将无法完成并将继续侦听和发出更改。
这个实现可能是可以接受的。你说"seems a little labor intensive not to mention slow and getting slower",但是你剖析过吗?
在不了解您的应用程序的情况下,很难提供建议。例如,您提到它的"getting slower the more records are held locally",但是一旦您检查了数据库中是否存在一条记录,您是否可以不将其存储在本地?如果你能做到这一点,也许你没有问题?