1
votes

I am writing a chat application and attempting to implement readReceipts. I have the following Firebase database structure, and when a message is opened, I will do a updateChildValue. This will update the Bool to true, like so:

Snap (messages) {
    "-L7E-3km6bvx3WWFHeuj" = 1;
    "-L7E-MXzwmZ9iv6Bj0oM" = 1;
    "-L7E-N5pm-FGtb3m3AOr" = 1;
    "-L7E-Q2cRJz9DDFgRWL_" = 1;
    "-L7E-U4r1vd94i-xbNTh" = 1;
    "-L7E1LdrJaqZDjIkWd7X" = 1;
    "-L7EBz3hxfJ-E3f7joJ6" = 1;
    "-L7EE8A5eZNyey12GcHB" = 1;
    "-L7EEwX2m-rrPYFgDoA8" = 0;
    "-L7EFA7CHfyRoECUkeLm" = 0;
    "-L7EFPICxdWQcrLOEUkM" = 0; //Updating this will change Bool to 1
    "-L7EJ1byG2KLKZf_Cspv" = 0;
    "-L7EKRmXYz_94UrIPGhS" = 0;
}

I listen for childChanged on my messages node, and when the update changes to true, I will update the views of the collectionViewCells. However, implementing childChanged will always extract the whole messages node.

Is there a way to identify the exactly which id gets the update and hence obtain the indexPath to update the collectionViewCells? Alternatively, is there a better way to implement readReceipts?

1
The childChanged event fires on the child node that changed under the location where you listen. If that doesn't seem to work for you, post the minimal, standalone code that reproduces the problem. - Frank van Puffelen

1 Answers

1
votes

There is another observer function just for your case

from Firebase Docs

observeEventType:andPreviousSiblingKeyWithBlock: is used to listen for data changes at a particular location. This is the primary way to read data from the Firebase Database. Your block will be triggered for the initial data and again whenever the data changes. In addition, for FIRDataEventTypeChildAdded, FIRDataEventTypeChildMoved, and FIRDataEventTypeChildChanged events, your block will be passed the key of the previous node by priority order.

func observe(_ eventType: DataEventType, andPreviousSiblingKeyWith block: @escaping (DataSnapshot, String?) -> Void) -> UInt

Example:

Database.database().reference().child("messages").observe(.childChanged) { (snapshot, key) in
    print(key)
    //"-L7EFPICxdWQcrLOEUkM"
}