【发布时间】:2017-01-18 14:48:18
【问题描述】:
我这几天一直在参考这个 SO 帖子: Filtering results with Geofire + Firebase
我的问题是,对于我的 iOS 应用程序,我需要制作一个按凭据排序的附近用户的单一列表,例如:高级会员(最高且位于列表顶部)、捐赠者(仅次于高级)、会员(基本/最低)。
我在我的 Firebase 服务器中为 GeoFire 位置创建了 3 个条目,这些条目根据这些凭据拆分用户,因此需要运行 3 个查询来检索它们。
GeoFire* geoFirePremium = [[GeoFire alloc] initWithFirebaseRef:[[[FIRDatabase database] reference] child:@"geofire-premium-members"]];
GeoFire* geoFireDonator = [[GeoFire alloc] initWithFirebaseRef:[[[FIRDatabase database] reference] child:@"geofire-donator-members"]];
GeoFire* geoFireRegular = [[GeoFire alloc] initWithFirebaseRef:[[[FIRDatabase database] reference] child:@"geofire-regular-members"]];
NSMutableDictionary* query1Items = [[NSMutableDictionary alloc] init];
NSMutableDictionary* query2Items = [[NSMutableDictionary alloc] init];
NSMutableDictionary* query3Items = [[NSMutableDictionary alloc] init];
CLLocation* coord = [[CLLocation alloc] initWithLatitude:34.2499 longitude:-85.4399]; // Test location
long searchDistance = 8;
float mile2Kilo = 1.60934;
float kilo2mile = 0.62137;
GFCircleQuery* query1 = [geoFirePremium queryAtLocation:coord withRadius:(CGFloat)(searchDistance * mile2Kilo)]; // Miles to Kilometers
[query1 observeEventType:GFEventTypeKeyEntered withBlock:^(NSString* key, CLLocation* location)
{
// Store results in query1Items
}];
GFCircleQuery* query2 = [geoFireDonator queryAtLocation:coord withRadius:(CGFloat)(searchDistance * mile2Kilo)];
[query2 observeEventType:GFEventTypeKeyEntered withBlock:^(NSString* key, CLLocation* location)
{
// Store results in query2Items
}];
GFCircleQuery* query3 = [geoFireRegular queryAtLocation:coord withRadius:(CGFloat)(searchDistance * mile2Kilo)];
[query3 observeEventType:GFEventTypeKeyEntered withBlock:^(NSString* key, CLLocation* location)
{
// Store results in query3Items
}];
我的想法是添加一些代码来识别所有 3 个查询何时完成,然后将它们合并到 1 个列表中。
NSMutableDictionary* mergedItems = [[NSMutableDictionary alloc] init];
// For example: { query1Items[], query2Items[], query3Items[], ... }
[query1 observeReadyWithBlock:^{
NSLog(@"Query 1 is finished");
// Check for queries 2 & 3 completion
// Perform merge if all are completed
}];
[query2 observeReadyWithBlock:^{
NSLog(@"Query 2 is finished");
// Check for queries 1 & 3 completion
// Perform merge if all are completed
}];
[query3 observeReadyWithBlock:^{
NSLog(@"Query 3 is finished");
// Check for queries 1 & 2 completion
// Perform merge if all are completed
}];
所有 Firebase/GeoFire 引用的 JSON 结构如下:
- geofire-premium-members
- userid
- g: geohash
- l
- 0: lat
- 1: lon
- geofire-donator-members //same format
- geofire-regular-members //same format
- users
- userid
- …
像这样使用多个查询是一种好方法吗?将来我可能需要添加更多凭据,并且不知道我的方法是否可以很好地扩展。是否有更好的方法来实现我需要的可能只使用单个查询来代替?我非常感谢任何见解
【问题讨论】:
-
我们大多数人发现解析代码比解析描述该代码的单词更容易。如果您对当前代码的结构有疑虑,请分享重现这些疑虑的最少代码。如果您随后还添加 JSON 结构的 sn-p(作为文本,请不要截图),您更有可能得到有用的答案。
-
@FrankvanPuffelen 谢谢,我已经更新了相关代码
标签: ios objective-c firebase firebase-realtime-database geofire