I've been referring to this SO post for a few days now: Filtering results with Geofire + Firebase
My issue is that for my iOS app I need to make a single list of nearby users that is ordered by credentials, for example: premium member (highest and at top of list), donator (next highest after premium), member (basic/lowest).
I've created 3 entries in my Firebase server for GeoFire locations that split users based on these credentials, and therefore need to run 3 queries to retrieve them.
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
}];
My thoughts are to add some code that recognizes when all 3 queries complete and then merges them into 1 list.
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
}];
Where the JSON structure for all Firebase/GeoFire references follows:
- geofire-premium-members
- userid
- g: geohash
- l
- 0: lat
- 1: lon
- geofire-donator-members //same format
- geofire-regular-members //same format
- users
- userid
- …
Is it a good approach to use multiple queries like this? It is possible I may need to add more credentials in the future and do not know if my approach will scale well. Is there perhaps a better way to achieve what I need that maybe only uses a single query instead? I greatly appreciate any insight