0

The question seems easy bit it is a bit more complicated. Here is the relevant database structure:

"people" : {
    "q3up6CyAe5PoEAooJwCclOGGcZd2" : {
      "peopleWhoLike2" : {
        "NMNQYJ5z64fATK2MMs7m0ggHl0k2" : 1571089433551

We are interested in the 1571089433551 timestamp. The first user id (q3up6CyAe5PoEAooJwCclOGGcZd2) is the user whom the current user liked. The current user is NMNQYJ5z64fATK2MMs7m0ggHl0k2

We want to write an if statement for when the current time minus 1571089433551 is larger than 8000 seconds. That part I can do. The question is not for that. It is for how to get to 1571089433551 as an int via a snapshot

This is how the timestamp was made:

      ref.child("people").child(self.postID).observeSingleEvent(of: .value, with:  {(snapshot) in
            if let people1 = snapshot.value as? [String: AnyObject] {

                let current = Auth.auth().currentUser!.uid
                let p3 = ServerValue.timestamp()as! [String : Any]
                let updateLikes: [String: Any] = [current: p3]

                ref.child("people").child(self.postID).child("peopleWhoLike2").updateChildValues(updateLikes, withCompletionBlock: { (error, reff) in .....

This is where I tried to user it for the IF statement

          let thisUsersUid = Auth.auth().currentUser?.uid //Mr. Dunn's uid

          refArtists = Database.database().reference().child("people").child(self.postID).child("peopleWhoLike2")

          refArtists.observe(DataEventType.value,  with: {snapshot in

    ---------


let secondsInDay = 86400

if (Int(-date.timeIntervalSinceNow) < secondsInDay){

That ------ part between the above is what I need to figure out. How to read the firebase snapshot and get the 1571089433551 as an int.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441

1 Answers1

0

This is a literal answer - I suspect there may be more to it so I can update as needed.

Start with a structure similar to yours

people
   uid_0 //the user whom the current user liked,
      peopleWholike2
         uid_1: 123456 //the current user with a timestamp

Here's one way to get directly to the timestamp: 123456. I added some extra vars so you can see the path and self.ref points to my Firebase.

func readChildTimestamp() {
    let peopleRef = self.ref.child("people")
    let thisPersonRef = peopleRef.child("uid_1")
    let peopleThatLikeThisPersonRef = thisPersonRef.child("peopleWhoLike2")
    peopleThatLikeThisPersonRef.observeSingleEvent(of: .childAdded, with: { snapshot in
        let personUidKey = snapshot.key
        let timestamp = snapshot.value as? Int ?? 0
        print(personUidKey, timestamp)
    })
}

I leveraged .childAdded in this case because it only will ever read the first node when used with .observeSingleEvent. In your question you are looking for that specific timestamp so this will do it.

Edit:

Based on some additional information we need to expand this answer. It appears there may be multiple child nodes within the peopleWhoLikeMe2 node like this

people
   uid_0
      peopleWholike2
         uid_1: 123
         uid_2: 456
         uid_3: 789

Suppose user with uid_3 logs in and wants to read their timestamp from uid_0's node. Here's the code

func readChildTimestamp() {
    let myUid = "uid_3" //this is the currently logged in user
    let peopleRef = self.ref.child("people")
    let thisPersonRef = peopleRef.child("uid_0")
    let peopleThatLikeThisPersonRef = thisPersonRef.child("peopleWhoLike2")
    let myRef = peopleThatLikeThisPersonRef.child(myUid)
    myRef.observeSingleEvent(of: .value, with: { snapshot in
        let personUidKey = snapshot.key
        let timestamp = snapshot.value as? Int ?? 0
        print(personUidKey, timestamp)
    })
}

and the out is

uid_3 789
Jay
  • 34,438
  • 18
  • 52
  • 81
  • Thanks. It makes sense that the big error was using only .observeSingleEvent. I am still getting this error though: Terminating app due to uncaught exception 'InvalidPathValidation', reason: '(child:) Must be a non-empty string and not contain '.' '#' '$' '[' or ']'' – SeattleSurfer Oct 15 '19 at 22:08
  • do you know how to fix that. I suspect it is the line of: .childAdded – SeattleSurfer Oct 15 '19 at 22:08
  • scratch that. It was my mistake. I wrongly added "" to self.uid1 in let thisPersonRef = peopleRef.child("uid_1") – SeattleSurfer Oct 15 '19 at 22:31
  • @SeattleSurfer Did the answer help or do I need to update further? – Jay Oct 16 '19 at 15:37
  • Nah, perfect. Sorry forgot yesterday to mark as the correct answer. – SeattleSurfer Oct 16 '19 at 19:59
  • BTW, just to take this forward. Now that I can access the timestamp: how can I convert it to NSDate in Xcode and check via "if statement" if that date is further than today? – SeattleSurfer Oct 16 '19 at 22:36
  • @SeattleSurfer That would be a separate question. However, there are dozens of examples here on SO on working with NSDate's - you can do the convert using a DateFormatter (NSDateFormatter) [here](https://stackoverflow.com/questions/28404594/swift-cant-get-nsdate-dateformatter-right) [or here](https://stackoverflow.com/questions/41933710/how-to-get-int-from-dateformatter-in-swift3) or even DateComponents and check out this [awesome answer](https://stackoverflow.com/questions/37923481/convert-date-string-swift) – Jay Oct 17 '19 at 13:57
  • Just read your last line again. That is causing issue now that there is more than one line, ie more than one user who liked. It should check the user who is logged in not just the first like uid1. How can you do that with the code you provided? – SeattleSurfer Oct 20 '19 at 22:07
  • @SeattleSurfer I can address that but need a bit more info. The question asks about that specific line *We are interested in the 1571089433551 timestamp. The first user id is the user whom the current user liked*. Under what circumstances would you want the timestamp from a different user within that node - or do you want all of them? – Jay Oct 21 '19 at 14:07
  • my apologies for being unclear. So by first user id, I mean the one under people, ie (q3up6CyAe5PoEAooJwCclOGGcZd2). That is the use who current user likes. Then current user is NMNQYJ5z64fATK2MMs7m0ggHl0k2 together with the timestamp. All of the user who like the one under people will get timestamps. On each of their phones, they are current user. Does that make sense? – SeattleSurfer Oct 21 '19 at 18:57
  • @SeattleSurfer I think I understand but need more info. Take a look at the edit in my answer; I think you're saying as more people like uid_0, they are added into the child nodes of peopleWhoLike. Correct? If so, under what circumstances would you want the timestamp of uid_2 for example. What would trigger that - I am asking because the code to do that will depend on what the reason is. For example, uid_2 logs in and wants to know at what time they liked uid_0. Is that correct? – Jay Oct 21 '19 at 20:29
  • < I think you're saying as more people like uid_0, they are added into the child nodes of peopleWhoLike> Correct. Child nodes of peopleWhoLike2. You would want uid_2 in the case that he is the current user and tries to like uid_0. We want it to be so that he only gets to like uid_0 once per day, so it is necessary to keep track of who and when liked uid_0. The child entries of uid_1, uid_2 etc all occur when they are the current user on their phone and like uid_0. Does that make sense? – SeattleSurfer Oct 21 '19 at 22:47
  • @SeattleSurfer I think I understand - updated the answer with additional code. – Jay Oct 22 '19 at 16:56