0

I'm developing a chat app. In my app there are 4 nodes called User, Recent, Message, Group. I'm using Objective-C My message object looks like,

{
    "createdAt" : 1.486618017521277E9,
    "groupId" : "-KcWKeXXQ9tjYsYfCknx",
    "objectId" : "-KcWKftK8GiMxxAnarL5",
    "senderId" : "828949592937598976",
    "senderImage" : "http://hairstyleonpoint.com/wp-content/uploads/2014/10/marcello-alvarez.png",
    "senderName" : "John Doee",
    "status" : "Seen",
    "text" : "Hi all",
    "type" : "text",
    "updatedAt" : 1.486622011467733E9
}

When I'm updating a User, all message's senderName should be updated accordingly. Is there are way to do this via the code or Do I need to write a rule. I'm a newbie to the firebase. Please suggest me a way to do that. If It's possible to do with the rules, Please guide me on this.

codebot
  • 2,540
  • 3
  • 38
  • 89
  • 2
    You'll have to update it manually. There is no code that will do it for you. – Mathew Berg Feb 09 '17 at 13:01
  • See my answer here for the possible approaches: http://stackoverflow.com/questions/30693785/how-to-write-denormalized-data-in-firebase/30699277#30699277. The answer is for JavaScript, but the approaches are valid on any platform. – Frank van Puffelen Feb 09 '17 at 14:47

1 Answers1

2

It's not possible to do this via rules, so you have to manually iterate over all your data and update the senderName.

Anyways, I think you would probably be better off with saving {senderID: $someUserID} instead - like you would do in a relational database. The userID is static, so can change the user without having to update all the instances where you use it.

Lukasstr
  • 435
  • 2
  • 19