Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/109.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 更新Firebase将删除现有数据_Ios_Objective C_Firebase - Fatal编程技术网

Ios 更新Firebase将删除现有数据

Ios 更新Firebase将删除现有数据,ios,objective-c,firebase,Ios,Objective C,Firebase,我正在用Firebase构建一个应用程序,最近我用这段代码更新了用户字典中的“帖子”列表/字典。看一看: Firebase *baseRef = [[Firebase alloc] initWithUrl:@"https://<MY-APP>.firebaseio.com"]; NSString *userPostsPath = [NSString stringWithFormat:@"users/%@/posts", userId]; NSDictionary *postRef

我正在用Firebase构建一个应用程序,最近我用这段代码更新了用户字典中的“帖子”列表/字典。看一看:

Firebase *baseRef = [[Firebase alloc] initWithUrl:@"https://<MY-APP>.firebaseio.com"];

NSString *userPostsPath = [NSString stringWithFormat:@"users/%@/posts", userId];

NSDictionary *postRef = @{ postId : postDescription };

[baseRef updateChildValues:@{
                             userPostsPath : postRef
                             // here I'd have a similar update
                             }withCompletionBlock:^(NSError *error, Firebase *ref) {
                                 handler(error, ref);
                             }];
正确更新Firebase

两者之间有什么区别?为什么第一个不起作用

编辑

是否有可能做到:

Firebase *baseRef = [[Firebase alloc] initWithUrl:@"https://<MY-APP>.firebaseio.com"];
NSString *newKey = [baseRef childByAutoId];
Firebase*baseRef=[[Firebase alloc]initWithUrl:@”https://.firebaseio.com"];
NSString*newKey=[baseRef childByAutoId];
然后使用该键执行如下更新:

[baseRef updateChildValues:@{
                           [NSString stringWithFormat:@"/posts/%@/", newKey] : // something
                           [NSString stringWithFormat:@"/posts/%@/members/<something>", newKey] : // something
withCompletionBlock:^(NSError *error, Firebase *ref) {
            handler(error, ref, task);
        }];
[baseRef UpdateChildValue:@{
[NSString stringWithFormat:@/posts/%@',newKey]://什么
[NSString stringWithFormat:@/posts/%@/members/”,newKey]://什么
withCompletionBlock:^(N错误*错误,Firebase*参考){
处理程序(错误、参考、任务);
}];

基本上是在同一请求中对同一路径发出多个更新,这在避免覆盖问题时是不存在的

您的第一个示例将转换为更新指令:

"users/posts" = { "postid1": postDescription }
第二个例子是:

"users/posts/postid1" = postDescription
作为一个计算机程序,Firebase服务器对您给出的指令进行相当字面的解释。它接受每条更新指令,并用值(位于
=
之后的部分)替换路径(位于
=
之前的部分)上的数据

知道了这一点,您可以看到,在第二个示例中,它将在
users/posts/postd1
处编写
postDescription
。这将替换该帖子的现有描述,但这可能是您的想法

第二个示例在
users/posts
处写入
{“postd1”:postDescription}
。这将替换该位置的现有值,因此实际上是将所有现有的posts替换为新的/更新的posts。这可能不是您想要的

更新


如果要创建一个新对象并将密钥展开到多个位置,可以利用
childByAutoId
是一个纯客户端操作的事实:

let newRef = ref.childByAutoId()
let newKey = newRef.key
ref.childByAppendingPath("child1").childByAppendingPath(newKey).setValue("one")
ref.childByAppendingPath("child2").childByAppendingPath(newKey).setValue("two")

您的第一个示例将转换为更新说明:

"users/posts" = { "postid1": postDescription }
第二个例子是:

"users/posts/postid1" = postDescription
作为一个计算机程序,Firebase服务器对您给出的指令进行相当字面的解释。它接受每条更新指令,并用值(位于
=
之后的部分)替换路径(位于
=
之前的部分)上的数据

知道了这一点,您可以看到,在第二个示例中,它将在
users/posts/postd1
处编写
postDescription
。这将替换该帖子的现有描述,但这可能是您的想法

第二个示例在
users/posts
处写入
{“postd1”:postDescription}
。这将替换该位置的现有值,因此实际上是将所有现有的posts替换为新的/更新的posts。这可能不是您想要的

更新


如果要创建一个新对象并将密钥展开到多个位置,可以利用
childByAutoId
是一个纯客户端操作的事实:

let newRef = ref.childByAutoId()
let newKey = newRef.key
ref.childByAppendingPath("child1").childByAppendingPath(newKey).setValue("one")
ref.childByAppendingPath("child2").childByAppendingPath(newKey).setValue("two")

如果我想创建两个新帖子或“路径”,最好的方法是什么在同一个调用中,虽然仍在使用
childByAutoId
?尝试尽可能高效地编写这些更新以提取最少的移动数据和资源
childByAutoId
是一个客户端操作,因此您可以从中获取密钥。我将编写一个快速片段,并将其添加到我的答案中。我明白了,获取
childB的算法是否正确yAutoId
在任何位置都保持不变?这意味着我可以
让newRef=ref.childByAutoId()
ref
是Firebase的根,并将该键用于不同的位置吗?这样,如果出于某种原因需要,您可以“模拟”一个新的Firebase引用并使用它来获得一个新的唯一密钥?Firebase创建的推送ID在这里描述:如果我想创建两个新帖子或“路径”,最好的方法是什么在同一个调用中,虽然仍在使用
childByAutoId
?尝试尽可能高效地编写这些更新以提取最少的移动数据和资源
childByAutoId
是一个客户端操作,因此您可以从中获取密钥。我将编写一个快速片段,并将其添加到我的答案中。我明白了,获取
childB的算法是否正确yAutoId
在任何位置都保持不变?这意味着我可以
让newRef=ref.childByAutoId()
ref
是Firebase的根,并将该键用于不同的位置吗?这样,如果出于某种原因需要,您可以“模拟”一个新的Firebase ref并使用它来获得一个新的唯一密钥?Firebase创建的推送ID如下所述: