本文介绍了GeoFire更新在同一个电话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个Firebase更新字典,如下所示:

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

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

但是我也想在同一个更新中保存一个位置的坐标,做2个独立的电话。我已经看过 Github页面,但是我无法弄清楚如何做这样的事情

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

和我的第一个例子一样吗?

有没有这样做的首选方法,还是我需要自己做些什么?例如更改库或将 CLLocation 设置为任何其他变量的更新?就像这样:

pre $ - (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]};
$ b $ ref [setValue:update withCompletionBlock:^(NSError * error,Firebase * ref){
// complete
}];

编辑

这是一段与我的项目非常相似的代码:

   - (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
}];


解决方案



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

您还将设置节点的优先级,这是GeoFire所需的(afaik)。 / p>

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
        }];
}
解决方案

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.

这篇关于GeoFire更新在同一个电话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 01:26