我正在开发一个应用程序,承包商可以说它们在特定日期“可用”,每个承包商都有一个“位置”。雇主可以根据位置和可用性搜索可用性。
该位置基于GeoFire。这将返回可用承包商的$ key。
看起来像这样:
geoQueryContractor(radius, lat, lng) {
const subject = new Subject();
this.fbGeoRef = firebase.database().ref('geofire')
this.geoFire = new GeoFire(this.fbGeoRef);
this.geoFire.ref();
this.geoQuery = this.geoFire.query({
center: [lat, lng],
radius: radius
});
this.geoQuery.on("key_entered", function(key, location, distance) {
subject.next(key);
});
return subject.asObservable();
}
接下来,我可以通过搜索看起来像这样的“/ availForContractor / $ {timestamp} / $ uid:true”的firebase节点来获取可用性
这是如何工作的,它会返回他们的个人资料:
getAvailablitybyContractor(timestamp) {
const availContractorKeys$ = this.db.list(`/AvailForContractor/${timestamp}`);
const AvailContractors$ = availContractorKeys$
//maping each key
.map(keysPerContractor => keysPerContractor
//once we have each key, we can map it and create an fb object observable
.map(keyPerContractor => this.db.object(`/users/${keyPerContractor.$key}`)))
//now we got back an array of firebase object observables (fbojs) and we need to combine them in to one observable
.mergeMap(fbojs => Observable.combineLatest(fbojs))
.do(console.log)
AvailContractors$.subscribe();
}
我让这两个人彼此独立工作。我真的需要知道第二个函数中返回的所有$ key,哪些是第一个函数中的位置。我只需要返回符合这两个条件的配置文件。
我一直在搞乱CombineLatest,mergeMap,withLatestFrom和Filter,但我无法弄清楚如何以正确的方式做到这一点。
我的想法是,一旦我从第二个功能获得键,将它与GeoFire observable结合并过滤唯一键,然后执行此部分:
//once we have each key, we can map it and create an fb object observable
.map(keyPerContractor => this.db.object(`/users/${keyPerContractor.$key}`)))
//now we got back an array of firebase object observables (fbojs) and we need to combine them in to one observable
.mergeMap(fbojs => Observable.combineLatest(fbojs))
.do(console.log)
这不完整,但尝试很差......
getAvailablitybyContractor(timestamp, radius, lat, lng) {
const availContractorKeys$ = this.db.list(`/AvailForContractor/${timestamp}`);
//when we get back the keys, we are going to switch to another obeservables
const AvailContractors$ = availContractorKeys$
//maping each key
.map(keysPerContractor => keysPerContractor
.map(keyPerContractor => keyPerContractor.$key))
.combineLatest(this.geoQueryContractor(radius, lat, lng))
// .withLatestFrom(this.geoQueryContractor(radius, lat, lng), (keysPerContractor, geo) => ( [keysPerContractor, geo] ))
//once we have each key, we can map it and create an fb object observable
// .map(keyPerContractor => this.db.object(`/users/${keyPerContractor.$key}`)))
// //now we got back an array of firebase object observables (fbojs) and we need to combine them in to one observable
// .mergeMap(fbojs => Observable.combineLatest(fbojs))
.do(console.log)
AvailContractors$.subscribe();
}
GeoFire顺便推出了这样的各个键:
3vAWWHaxHRZ94tc8yY08CH3QNQy3
,H74INXgYWIMrUcAtZloFGkwJ6Qd2
等
Firebase将推出一系列密钥:
[3vAWWHaxHRZ94tc8yY08CH3QNQy3, H74INXgYWIMrUcAtZloFGkwJ6Qd2, J9DHhg5VQrMpNyAN8ElCWyMWh8i2, fdZYKqqiL0bSVF66zGjBhQVu9Hf1 ]
最终结果将是我用于获取配置文件的RX方式的唯一组合。
有人可以帮忙吗?谢谢!
答案 0 :(得分:0)
这是我的解决方案。我确信有更好的方法可以做到这一点。更新:下面更好的解决方案。
static geoArray: Array<string> = [];
constructor(private af: AngularFire, private db: AngularFireDatabase) {
}
getAvailablitybyContractor(timestamp, radius, lat, lng) {
const availContractorKeys$ = this.db.list(`/AvailForContractor/${timestamp}`);
const AvailContractors$ = availContractorKeys$
//maping each key
.map(keysPerContractor => keysPerContractor.map(keyPerContractor => keyPerContractor.$key)
.filter(key => ContractorService.geoArray.indexOf(key) > -1)
//once we have each key, we can map it and create an fb object observable
.map(keyPerContractor => this.db.object(`/users/${keyPerContractor}`)))
//now we got back an array of firebase object observables (fbojs) and we need to combine them in to one observable
.mergeMap(fbojs => Observable.combineLatest(fbojs))
.do(console.log)
AvailContractors$.subscribe();
}
geoQueryContractor(radius, lat, lng) {
this.fbGeoRef = firebase.database().ref('geofire')
this.geoFire = new GeoFire(this.fbGeoRef);
this.geoFire.ref();
this.geoQuery = this.geoFire.query({
center: [lat, lng],
radius: radius
});
this.geoQuery.on("key_entered", function(key, location, distance) {
ContractorService.geoArray.push(key);
});
}
}
这个解决方案要好得多。上面的那个真的很麻烦。它与清除阵列有关。在一天结束时,我将不得不执行搜索2x以获得正确的结果。不能接受。
这是一种更好的方法,它更具反应性,并且没有上面的错误。我确信有更好的方法来折射或改进它。
getAvailablitybyContractor(timestamp) {
let availContractorKeys$ = this.db.list(`/AvailForContractor/${timestamp}`);
this.AvailContractors$ = availContractorKeys$
//maping each key
.map(keysPerContractor => keysPerContractor.map(keyPerContractor => keyPerContractor.$key))
//Combine observable from GeoQuery
.combineLatest(this.keys$, (fb, geo) => ([fb, geo]))
// fb, geo are accessible individually
// .filter method creates a new array with all elements that pass the test implemented by the provided function
// key is now iteriable through geo.indexOf
.map(([fb, geo]) => {
return fb.filter(key => geo.indexOf(key) > -1)
})
.map(filteredKeys => filteredKeys.map(keyPerContractor => this.db.object(`/users/${keyPerContractor}`)))
//now we got back an array of firebase object observables (fbojs) and we need to combine them in to one observable
.mergeMap(fbojs => {
return Observable.combineLatest(fbojs)
})
.do(console.log)
}
getGeoQuery(radius, lat, lng) {
this.geoQuery = this.geoFire.query({
center: [lat, lng],
radius: radius
});
}
geoQueryContractor() {
return this.keys$ = Observable.create(observer => {
var keys = new Array();
this.geoQuery.on("key_entered", (key, location, distance) => {
keys.push(key);
observer.next(keys);
});
});
}