0

So, when Single Event is used in the first snapshot below (see bold), array1 (coming from the 2nd snapshot) can't be picked up by the first snapshot. But when .observe is used instead, it can. Why could that be?

At first I couldn't understand why Single Even fails, but .observe doesn't. I made a lot of prints and found that the 2nd snapshot works just fine. But array1 in the second snapshot is not recognized if .SingleEvent is used. It is, however, recognized if .observe is used instead.

/////first snapshot

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

refArtists = Database.database().reference().child("people");

**refArtists.observeSingleEvent**(of: .value,  with: { [weak self]snapshot in


if snapshot.childrenCount>0{

self.people.removeAll()

for people in snapshot.children.allObjects as! [DataSnapshot] {
    
    if people.key != thisUsersUid {
        print("peoplekey",people.key)
        
        let peopleObject = people.value as? [String: AnyObject]
        let peopleEducation = peopleObject?["Education"] as? String
        ...
        let userId = people.key
        
        ...
        
        if Calendar.current.isDateInToday(date) {
            let distance = locCoord.distance(from: self.dict)
            print(distance, "distancexy")
            
            if distance/1609.344 < 3000 && self.array1.contains(people.key){
                print(self.array1, "f111111")
                
                print("fee", self.dict )
                print(distance, "distancexy")
                
                let peopl = Userx(Education: peopleEducation, .......)
                
                self.people.append(peopl)
                let d = people.key
                self.printPersonInfo(uid:d)
                
            } else {
                print ("w")
            }
        } else {
            print ("alpha")
        }
    }

    print("aaaaaaaa", self.people.map {$0.distance})
}
self.people.sort { ($0.distance ?? 0) < ($1.distance ?? 0) }
}
})

//////2nd snapshot that makes array1 for 1st snapshot

    guard let myUid = Auth.auth().currentUser?.uid else { return }

        refArtists = Database.database().reference().child("people").child(myUid).child("e2")
        
        refArtists.observeSingleEvent(of:.value,  with: {snapshot in
            
           let myDomain = snapshot.value as? String
            self.bSnap = myDomain
            print("haaal", self.bSnap)

            
            
                           let peopleRef = Database.database().reference().child("people")
                           let thisPersonRef = peopleRef.child(myUid).child("e2")
                           thisPersonRef.observeSingleEvent(of:.value,  with: {snapshot in
                               
                                   if snapshot.exists() {

        

            let query = Database.database().reference().child("people").queryOrdered(byChild: "e2").queryEqual(toValue: self.bSnap)
          query.observeSingleEvent(of: .value, with: { snapshot in
              var allUsers = snapshot.children.allObjects as! [DataSnapshot]
              ///////end (1) of comment
              if let index = allUsers.firstIndex(where: { $0.key == myUid } ) {
                  allUsers.remove(at: index) //remove the current user
              } /////end (2) of comment
                
                for userSnap in allUsers {
                    let name = userSnap.childSnapshot(forPath: "postID").value as? String
                    print(name, "NNN")

                if let unwrappedName = name {
                    **self.array1.append(unwrappedName)**
                }
                }

            print(self.array1, "ahah")
                
              
            
            
            })
                                    
                                   } else {
                            print("no")
                                    
                            }
          })

      })
uuuuuu
  • 119
  • 1
  • 7
  • The code is really poorly formatted so it's hard to read. Also, there's what appears to be a bunch of unrelated code so that could probably be removed for this question. The biggest issue is that you're asking about .observe vs .observeSingleEvent (I think) but there is no .observe shown in the provided code. What is *first* vs *second* snapshot as there are several. Can you update, shorten and clarify the question? Please take a moment and review [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) – Jay Oct 12 '20 at 18:00
  • I would also like to mention you've not indicated what, if anything your queries return or what you're Firebase structure looks like. For example, this is a pretty specific query `let query = Database.database().reference().child("people").queryOrdered(byChild: "e2").queryEqual(toValue: self.bSnap)` but there's no indication of what you're expecting in the snapshot. Is it a list of users? What does that look like? – Jay Oct 12 '20 at 20:53

0 Answers0