适用于多个位置的Firebase updateChildValue

时间:2017-06-26 08:03:04

标签: ios objective-c firebase firebase-realtime-database

如何updateChildValue多个child?如下例所示,但具有单个事务。保持key3完整

/*
user = {
    user1 : {
        key1 : -1,
        key2 : -1,
        key3 : 111,
    },
    user2 : {
        key1 : -1,
        key2 : -1,
        key3 : 111,
    },
}
*/

NSDictionary <NSString *, NSNumber *> *dictionary = @{
    @"key1" : @111,
    @"key2" : @111,
};

FIRDatabaseReference *ref = [[FIRDatabase database] reference];

[[ref
    child:@"user1"]
    updateChildValues:dictionary];

[[ref
    child:@"user2"]
    updateChildValues:dictionary];

1 个答案:

答案 0 :(得分:1)

要通过对updateChildValues的一次调用获得相同的结果,请展开字典以包含所有更新及其完整路径:

NSDictionary <NSString *, NSNumber *> *dictionary = @{
    @"user1/key1" : @111,
    @"user1/key2" : @111,
    @"user2/key1" : @111,
    @"user2/key2" : @111,
};

FIRDatabaseReference *ref = [[FIRDatabase database] reference];

[ref
    updateChildValues:dictionary];

另见Firebase blog post introducing multi-location updates