Search code examples
iosobjective-cfirebasegeofire

Get all objects within specific range in GeoFire


Is it possible to retrieve all keys within the specific range, using Firebase's GeoFire? I am aware that you can receive events for geo queries, like: key entered, key exited or key moved, however I am currently looking for something more like FEventTypeValue (one time value read, for the specific geo region), because my map objects are not moving.

Can't find anything in the docs: https://github.com/firebase/geofire-objc


Solution

  • You can use the key entered events to first save all keys for a query in a dictionary and then use the ready event to determine when all keys have been added:

    NSMutableDictionary *allKeys = [NSMutableDictionary dictionary];
    [query observeEventType:GFEventTypeKeyEntered withBlock:^(NSString *key, CLLocation *location) {
        [allKeys setObject:location forKey:key];
    }];
    [query observeReadyWithBlock:^{
        // Create an immutable copy of the keys in the query
        NSDictionary *valueData = [allKeys copy];
        NSLog(@"All keys within a query: %@", valueData);
    }];
    

    Don't forget to clean up your listeners afterwards.