0

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
        }];
}
Erik
  • 2,500
  • 6
  • 28
  • 49
  • There is no way to update both the GeoFire location and the object itself in a single call at the moment. It could probably be done with a single [multi-location update](https://www.firebase.com/blog/2015-09-24-atomic-writes-and-more.html), but none of the GeoFire libraries currently use this feature. – Frank van Puffelen Apr 09 '16 at 04:24
  • @FrankvanPuffelen I see, could I perhaps use my sample code and rather use GeoFire for queries? – Erik Apr 09 '16 at 13:08
  • Sure. That should work as long as you make sure you [set the same properties/priorities that GeoFire does](https://github.com/firebase/geofire-objc/blob/master/GeoFire/Implementation/GeoFire.m#L83-L108). – Frank van Puffelen Apr 09 '16 at 14:15
  • @FrankvanPuffelen Nice, did so now. I'm retrieving a priority using `GFGeoHash` and applying it to my update as `@"g" : geoHash`, but how can I specify it as a priority when using multi-location updates? Looking at [this](http://stackoverflow.com/questions/31577915/what-does-priority-mean-in-firebase) question, it appears deprecated? – Erik Apr 10 '16 at 14:57
  • Priorities are not deprecated, although we recommend against finding new use-cases for them (since they're not needed). But even if they were deprecated, they still work and are required for GeoFire. You can set a priority in multi-location updates by having a child names `.priority`. – Frank van Puffelen Apr 10 '16 at 15:07
  • @FrankvanPuffelen I see. What do you mean by having child names `.priority`? I can't find anything about it when searching – Erik Apr 10 '16 at 15:25
  • If you have a property named `.priority` it will set the priority value of the node. See a.o. http://stackoverflow.com/questions/15739359/firebase-priority-value. If you're having trouble getting that to work, post the code you've tried (either in this question or in another one). – Frank van Puffelen Apr 10 '16 at 15:28
  • @FrankvanPuffelen I'm struggeling to see what `.priority` actually is and how to implement it correctly with obj-c. On the other hand, this makes a lot of sense: `setValue:andPriority:`. I've updated my question with my progression so far; a simplified method and update but in basic terms the same as in my real project – Erik Apr 10 '16 at 17:39

1 Answers1

0

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.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • I'm updating my Firebase ref with this information now, but your answer in [this question](http://stackoverflow.com/questions/35301799/geofire-query-on-user-location) says that I should keep the location related information separated from the location itself? – Erik Apr 11 '16 at 10:04
  • **Edit**: I see that I could still use my multi-location update, but I have to write the location to a different location than the actual relevant data? – Erik Apr 11 '16 at 10:09