0

Hi I looked through a lot of questions dealing with Firebase's asynchronous database, but no answer quite fixed my problem :

Here is a screenshot of my firebase's structure

Here is a picture of the Output

My for loop prints "BACK TO FALSE" 35 times in a row(number of snapshot.children) BEFORE entering into the if. Is there a way to force the program to wait for the firebase response? Thanks in advance.

        ref.child("Events").observe(.value, with: { (snapshot) in

            for child in snapshot.children{
                multiple = false
                print("REMISE A FALSE")

            if let result = snapshot.children.allObjects as? [DataSnapshot] {

                    for child in result {
                        var eventForClub = child.key as! String

                        self.ref.child("Events").child("\(eventForClub)").observe(.value, with: { (snapshot) in
                            for child in snapshot.children{

                                let snap = child as! DataSnapshot
                                let event = snap.value as? [String:Any]
                                let end_time_test = event?["end_time"] as? String
                                let name_time = event?["name"] as? String
                                print(end_time_test)

                                let dateFormatter = DateFormatter()
                                dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
                                dateFormatter.timeZone = TimeZone(abbreviation: "GMT+2:00")

                                let end = dateFormatter.date(from: end_time_test!)!

                                if(end.compare(self.currentDate!) == .orderedAscending || multiple == true)
                                {
                                    print("date erronée")

                                }

                else{
                print(multiple)
                print("GOOD")
                multiple = true}
KENdi
  • 7,576
  • 2
  • 16
  • 31
BigBurger
  • 71
  • 10
  • What is the code trying to do? Maybe I can help come up with a different way to achieve the functionality. – Jen Person Sep 27 '17 at 15:51
  • Hi Jen, i'm trying to display event information. So I have multiple events on the long-term and i need to display only the ones where the end_time isn't already passed so I compared it with currentDate. But I don't want to display ALL the events just the first one, I created a bool that remembers if an event has already been displayed. As you can see there are multiple "lists" of events in my Firebase, each list is for one club I need one event displayed for every club – BigBurger Sep 27 '17 at 16:06
  • Have you tried firebase query, as i remember i have already user query to fetch items from firebase: For eg. modify the query let ref = FIRDatabase.database().reference().child("thoughts").queryOrdered(byChild: "createdAt").queryEqual(toValue : "Today") ref.observe(.value, with:{ (snapshot: FIRDataSnapshot) in for snap in snapshot.children { print((snap as! FIRDataSnapshot).key) } }) – Singh_Nindi Sep 27 '17 at 17:57
  • Or if let item = snapshot.value as? String{ self.changedName = item } – Singh_Nindi Sep 27 '17 at 17:58
  • The retrieving of the data from firebase is good. The only problem is this multiple that can't be put back to false due to asynchronous response from firebase. I tried this but it did't work out https://stackoverflow.com/questions/35906568/wait-until-swift-for-loop-with-asynchronous-network-requests-finishes-executing – BigBurger Sep 28 '17 at 09:43

1 Answers1

0

If I understand correctly you have a Firebase event node and each event has a variable of "currentDate" is some date format? You want to display them by printing them out?

ref.child("Events").observe(.value, with: { (snapshot) in
  if snapshot.value is NSNull{
     //handles errors
  }
  else{
     if let event= snapshot.value as? [String: String]{ //could be AnyObject if you have more then just strings
      if let eventClub = event["eventForClub"] as? [String: String]//Could also be a dictionary{
          let currentDate = eventClub["currentDate"]
          let endTime = eventClub["end_time"]
          //compare
          //print if true/false
      }    
     }
  }
})
Torewin
  • 887
  • 8
  • 22
  • Sorry if I wasn't clear enough currentDate is the variable given by the device. Each event has a start time and a end time what i am doing in my code is that i'm going through each events for a given club and I only want one to represent the club. This is were the multiple variable comes in, if an event is already associated to a club multiple = true. multiple is supposed to go back at false (in the bigger for loop) but apparently my for loop prints "BACK TO FALSE" 35 times in a row. – BigBurger Sep 28 '17 at 09:05
  • How many events do you currently have in the database? All together. – Torewin Sep 28 '17 at 20:04