Firebase不更新值

时间:2017-04-12 09:48:27

标签: ios swift firebase firebase-realtime-database

我正在使用FirebaseManager单例类从Firebase获取数据。我在getAllCharts中调用了ViewController,这需要这些数据。我最初收到数据,但是当我在firebase中更新我的数据时,它不会更新应用中的值(getAllCharts() 从不调用)。

  

FirebaseManager:

final class FirebaseManager {

    static let sharedInstance = FirebaseManager()

    private init() {}

    private let reference = FIRDatabase.database().reference()

    private var charts = [ChartRepresentable]()

    func getAllCharts(completionHandler: ((_ charts: [ChartRepresentable]) -> ())? = nil) {

        guard let path = FirebasePath.allCharts.path else {
            return
        }

        guard charts.isEmpty else {
            completionHandler?(charts)
            return
        }

        reference.child(path).observe(.value, with: { (snapshot) in

            if snapshot.hasChildren() {

                var availableCharts = [ChartRepresentable]()

                for child in snapshot.children {

                    guard let chartSnapshot = child as? FIRDataSnapshot else {
                        continue
                    }

                    switch chartSnapshot.key {
                    case "bar":
                        availableGraphs.append(BarChart(snapshot: chartSnapshot))
                    case "line":
                        availableGraphs.append(LineChart(snapshot: chartSnapshot))
                    default: break
                    }
                }

                self.charts = availableCharts
                completionHandler?(self.charts)
            }
        })
    }
}
  

的ViewController:

class ViewController: UIViewController {

    var setupCards: [SetupCard]?

    override func viewDidLoad() {
        super.viewDidLoad()

        FirebaseManager.sharedInstance.getAllCharts { [weak self] (charts) in
            self?.setupCards = [SetupCard(charts: charts)]
            self?.setupView.collectionView.reloadData()
            self?.setupView.pageControl.numberOfPages = self?.setupCards?.count ?? 0
        }
}

如何获取更新?

0 个答案:

没有答案