0

Does not exit from the observe block.

Doesn't print hello or events.

Stuck on fetching from database.

Displays the data from Firebase but not outside the block.

For example :

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    let databaseRef = FIRDatabase.database().reference()
    databaseRef.child("Events").queryOrderedByKey().observe(.childAdded, with: {
        snapshot in
        print(snapshot)
        var temp : event = event()
        temp.name = (snapshot.value as? NSDictionary)?["name"] as? String ?? ""
        temp.description = (snapshot.value as? NSDictionary)?["description"] as? String ?? ""
        temp.date = (snapshot.value as? NSDictionary)?["date"] as? String ?? ""
        temp.price = (snapshot.value as? NSDictionary)?["price"] as? String ?? ""
        temp.venue = (snapshot.value as? NSDictionary)?["venue"] as? String ?? ""
        temp.genre1 = (snapshot.value as? NSDictionary)?["genre1"] as? String ?? ""
        temp.genre2 = (snapshot.value as? NSDictionary)?["genre2"] as? String ?? ""
        temp.img = (snapshot.value as? NSDictionary)?["img"] as? String ?? ""

        self.events.append(temp)
        print(self.events)
    })
    print("Hello")
    print(self.events)
}

Thanks in advance!

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807

1 Answers1

1

Few things here:-

  • You are using, childAdded as the FIRDataEventType, and this query will only be fired when your Events node will get a value appended. If you want to access the present data in your Database node use value .

    databaseRef.child("Events").queryOrderedByKey().observe(.value, with: {
    
  • print(self.events) statement outside the completionBlock: will not work since the made to firebase database are asynchronous , but only the print(self.events) statement inside the completionBlock is only going to work.

  • If the case is that your viewDidLoad is not called then maybe you are not on the right viewController at the time.

Dravidian
  • 9,945
  • 3
  • 34
  • 74