michalbanan
michalbanan

Reputation: 165

Can't present items from Firebase Database in TableViewController

I can't present my objects obtained from Database. I declared array for containing my items to present:

var tournaments = [TournamentItem]()

My structure's TournamentItem init function:

init(snapshot: FIRDataSnapshot!) {

    key = snapshot.key
    ref = snapshot.ref
    type = snapshot.childSnapshotForPath("type").value as! String
    name = snapshot.childSnapshotForPath("name").value as! String
    numberOfParticipants = snapshot.childSnapshotForPath("numberOfParticipants").value as! Int
    tournamentMode = snapshot.childSnapshotForPath("mode").value as! Bool
    completed = snapshot.childSnapshotForPath("completed").value as! Bool
}

Function to obtaining data looks like:

func getTournamentListSnapshot() {

    ref.observeEventType(.Value, withBlock: { snapshot in

        //print("\(snapshot.key) -> \(snapshot.value)")


        for item in snapshot.children {

            let tournamentItem = TournamentItem(snapshot: item as! FIRDataSnapshot)
            self.tournaments.append(tournamentItem)

        }
        if self.tournaments.isEmpty { //not a subject of discussion
            self.showAlertOfEmptyTournamentsArray()
        }

    })
}

And i call this inside my ViewDidLoad method. The problem is that after calling this methods my array is empty but Firebase Database is not. I have tried to debug this whole thing but i can't find any solution to this problem.

My TableViewController delegate methods looks like this:

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {

    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return self.tournaments.count
}

Upvotes: 0

Views: 52

Answers (1)

Santosh
Santosh

Reputation: 2914

Call tableView.reloadData() after or may be in else:

if self.tournaments.isEmpty { 
  self.showAlertOfEmptyTournamentsArray() 
} 
tableView.reloadData()

Upvotes: 2

Related Questions