1

I am trying to store the Firebase server time using FIRServerValue.timestamp() with posts but noticed that my listener keeps on getting called twice. Printing in my console, I see the duplicate of the post but with two different timestamps milliseconds apart. Any help is greatly appreciated.

Listener:

DataService.ds.REF_POSTS.queryOrderedByChild("storeId").queryEqualToValue("\(self.specificId)").observeEventType(.Value, withBlock: {snapshot in

if let snapshot = snapshot.children.allObjects as? [FIRDataSnapshot] {
                        for snap in snapshot{
                            print("SNAP: \(snap)")
                            if let postDict = snap.value as? Dictionary<String, AnyObject> {
                                let key = snap.key
                                let post = Post(postKey: key, postData: postDict)
                                self.posts.insert(post, atIndex: 0)
                            }
                        }
                    }
            self.collection.reloadData()
        })

My posting:

        let post: Dictionary<String, AnyObject> = [
        "storeId": storeId!,
        "specificId": specificId.text!,
        "postedDate": FIRServerValue.timestamp()
    ]

    let firebasePost = DataService.ds.REF_POSTS.childByAutoId()
    firebasePost.setValue(post)
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
kelsheikh
  • 1,278
  • 3
  • 17
  • 37
  • 1
    It is normal that your value listener will be called twice for a server-side timestamp. The first call comes immediately when you write and contains the client-side estimate. The second call comes after the server has confirmed the actual value. I wrote this down into a nice answer a while ago in [this answer](http://stackoverflow.com/questions/37864974/how-to-use-the-firebase-server-timestamp-to-generate-date-created/37868163#37868163). While it is for Android, the same applies for all Firebase SDKs. – Frank van Puffelen Aug 10 '16 at 22:46
  • Excellent, thank you @FrankvanPuffelen – kelsheikh Aug 10 '16 at 23:09

0 Answers0