Search code examples
iosobjective-cfirebasegeofire

GeoFire update in same call


I'm creating a Firebase update dictionary like so

NSDictionary *update = @{
                        key1 : value1,
                        key2 : value 2,
                        key3 : value 3
                        };

[baseRef updateChildValues:update withCompletionBlock:^(NSError *error, Firebase *ref) {
            // complete
        }];

But I'd also like to save the coordinates of a location within the same update, aka not having to do 2 separate calls. I've taken a look at this Github page but I can't figure out how to do something like this

[geoFire setLocation:[[CLLocation alloc] initWithLatitude:37.7853889 longitude:-122.4056973]
          forKey:@"firebase-hq"];

in the same update as my first example?

Is there a preferred way to do this, or do I need to make something myself? E.g change the library or set a CLLocation to the update as any other variable? Something like this:

-(void)setLocation:(CLLocation *)location forRef:(Firebase *)ref
{
    NSNumber *lat = [NSNumber numberWithDouble:location.coordinate.latitude];
    NSNumber *lng = [NSNumber numberWithDouble:location.coordinate.longitude];
    NSDictionary *update = @{ @"location" : @[ lat, lng ] };

    [ref setValue:update withCompletionBlock:^(NSError *error, Firebase *ref) {
        // complete
    }];
}

Edit

here's a piece of code that's very similar to that in my project:

-(void)uploadUserId:(NSString *)userId withPlace:(GMSPlace *)place
{
    Firebase *ref = [[Firebase alloc] initWithUrl:baseRefUrl];

    NSNumber *lat = [NSNumber numberWithDouble:place.coordinate.latitude];
    NSNumber *lng = [NSNumber numberWithDouble:place.coordinate.longitude];
    NSString *geoHash = [GFGeoHash newWithLocation:place.coordinate].geoHashValue;

    NSDictionary *locationDetails = @{ @"l" : @[ lat, lng ], @"g" : geoHash};

    NSDictionary *update = @{
                                [NSString stringWithFormat:@"/users/%@/", userId] : locationDetails
                            };

    [ref updateChildValues:update withCompletionBlock:^(NSError *error, Firebase *ref) {
            // complete
        }];
}

Solution

  • If you update your dictionary to:

        NSDictionary *locationDetails = @{ 
            @"l" : @[ lat, lng ], 
            @"g" : geoHash,
            @".priority": geoHash
        };
    

    You will also be setting the priority of the node, which is (afaik) needed for GeoFire.