jlassap
jlassap

Reputation: 5

Swift - Pass data from firebase to another ViewController (segue)

I would like to pass only a few data from Firebase to another ViewController.

Below is my code. I've tested for several times but still failed. I don't know why the variable - "limits" turns out to be empty on the line print(limits) // IT'S EMPTY that I can't successfully pass the data to the next ViewController.

import UIKit
import Firebase
import FirebaseDatabase

struct limitStruct{
    let name : String!
    let today : String!
    let limit : String!

}

class CalcViewController: UITableViewController {

    var limits = [limitStruct]()

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        navigationController?.navigationBar.barTintColor = UIColor.black
        self.title = "Calculation"
        navigationController!.navigationBar.titleTextAttributes =
            [NSForegroundColorAttributeName: UIColor.white]
        let databaseReference = FIRDatabase.database().reference()

        databaseReference.child("Limit").queryOrderedByKey().observe(.childAdded, with: {
            snapshot in
            var snapshotValue = snapshot.value as? NSDictionary

            let name = snapshotValue!["name"] as? String
            snapshotValue = snapshot.value as? NSDictionary

            let today = snapshotValue!["today"] as? String
            snapshotValue = snapshot.value as? NSDictionary

            let limit = snapshotValue!["limit"] as? String
            snapshotValue = snapshot.value as? NSDictionary

            self.limits.insert(limitStruct(name:name, today:today, limit: limit), at: self.limits.count)
            self.tableView.reloadData()
        })

     print(limits) // IT'S EMPTY
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return limits.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "Limit")

        let label1 = cell?.viewWithTag(1) as! UILabel
        label1.text = limits[indexPath.row].name

        let label2 = cell?.viewWithTag(2) as! UILabel
        label2.text = limits[indexPath.row].today

        return cell!
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "showDetails"{

            let svc = segue.destination as! CirSliderViewController;

            if let indexPath = self.tableView.indexPathForSelectedRow{

                let segueData : limitStruct
                segueData = limits[indexPath.row]

                svc.RsegueData = segueData

            }

        }
    }
}

Upvotes: 0

Views: 923

Answers (1)

mlidal
mlidal

Reputation: 1153

databaseReference.child("Limit").queryOrderedByKey().observe() executes a block of code asynchronusly. This means that when print(limits) is called it has no yet been run, therefore limits is empty.

Upvotes: 1

Related Questions