Ios 一次更新多个节点Firebase

Ios 一次更新多个节点Firebase,ios,firebase,firebase-realtime-database,Ios,Firebase,Firebase Realtime Database,我正在构建聊天应用程序,并使用以下数据结构: 基本上,在user_chats中,我会跟踪用户参与的所有对话,以及该对话中的最后一条消息是什么,以便稍后我可以在表视图中显示 在messages节点中,我按自动ID存储给定对话的所有消息 现在,当用户发送消息时,它应该出现在给定会话的messages节点中,以及两个用户会话的最后一条消息中 在我使用调度组和单独呼叫Firebase之前,这似乎是非常不可靠和低效的 如何有效地同时更新所有值 额外:这种结构适合1-1聊天应用程序吗 使用工作代码更新: 我

我正在构建聊天应用程序,并使用以下数据结构:

基本上,在user_chats中,我会跟踪用户参与的所有对话,以及该对话中的最后一条消息是什么,以便稍后我可以在表视图中显示

在messages节点中,我按自动ID存储给定对话的所有消息

现在,当用户发送消息时,它应该出现在给定会话的messages节点中,以及两个用户会话的最后一条消息中

在我使用调度组和单独呼叫Firebase之前,这似乎是非常不可靠和低效的

如何有效地同时更新所有值

额外:这种结构适合1-1聊天应用程序吗

使用工作代码更新:

我已经使用扇出方法更新了所有的值

这是我的密码:

    NSDictionary *lastMessageDict = @{[self chatToUserID:userID] : @{@"last_message" : messageBody}};
    NSDictionary *singleMessageDict = @{@"body" : messageBody, @"time_stamp" : kTimeStamp, @"sender" : uID};
    NSString *autoID = [[self.databaseReference child:[NSString stringWithFormat:@"messages/%@/messages",[self chatToUserID:userID]]] childByAutoId].key;
    NSDictionary *messagesDict = @{[self chatToUserID:userID] : @{@"init" : facebookUserID , @"messages" : @{autoID : singleMessageDict}}};

    //Create fan out object
    NSMutableDictionary *fanOut = [NSMutableDictionary new];
    [fanOut addEntriesFromDictionary:@{[NSString stringWithFormat:@"user_chats/%@",uID] : lastMessageDict}];
    [fanOut addEntriesFromDictionary:@{[NSString stringWithFormat:@"user_chats/%@",userID] : lastMessageDict}];
    [fanOut addEntriesFromDictionary:@{@"messages" : messagesDict}];

    [self.databaseReference updateChildValues:fanOut withCompletionBlock:^(NSError *error,FIRDatabaseReference *reference){
        if(error){
            NSLog(@"Error: %@",error);
        }

        else{
            NSLog(@"updated!");
        }
    }];

在@Frank van Puffelen的帮助下,我使用扇出方法成功地完成了这项工作。这是我的密码:

NSDictionary *lastMessageDict = @{[self chatToUserID:userID] : @{@"last_message" : messageBody}};
NSDictionary *singleMessageDict = @{@"body" : messageBody, @"time_stamp" : kTimeStamp, @"sender" : uID};
NSString *autoID = [[self.databaseReference child:[NSString stringWithFormat:@"messages/%@/messages",[self chatToUserID:userID]]] childByAutoId].key;
NSDictionary *messagesDict = @{[self chatToUserID:userID] : @{@"init" : facebookUserID , @"messages" : @{autoID : singleMessageDict}}};

//Create fan out object
NSMutableDictionary *fanOut = [NSMutableDictionary new];
[fanOut addEntriesFromDictionary:@{[NSString stringWithFormat:@"user_chats/%@",uID] : lastMessageDict}];
[fanOut addEntriesFromDictionary:@{[NSString stringWithFormat:@"user_chats/%@",userID] : lastMessageDict}];
[fanOut addEntriesFromDictionary:@{@"messages" : messagesDict}];

[self.databaseReference updateChildValues:fanOut withCompletionBlock:^(NSError *error,FIRDatabaseReference *reference){
    if(error){
        NSLog(@"Error: %@",error);
    }

    else{
        NSLog(@"updated!");
    }
}];

您的问题中包含了JSON树的图片。请将其替换为实际的JSON文本,您可以通过单击中的导出JSON链接轻松获取。将JSON作为文本使其可搜索,允许我们轻松使用它来测试您的实际数据,并在我们的答案中使用它,这通常是一件好事。除此之外:是的,您可以使用单个多位置更新一次性发送更新。请参阅此博客文章:。如果您在使其工作时遇到困难,请发布您的代码。@FrankvanPuffelen非常感谢您!我让它工作了,我唯一不知道的是如何给每条消息一个唯一的ID。你能帮我吗?我更新了我的代码!只需生成一个唯一id为let uniqueId=self.databaseReference.childByAutoId.key的ref,然后在字典中的路径中使用该键。谢谢!更新了我的代码以供其他人查看。请将此作为答案发布,以便我可以给你打分!