从firebase数据库中检索计算平均值

时间:2017-05-17 11:23:19

标签: ios swift uitableview firebase firebase-realtime-database

我正在创建一个iOS应用程序,它使用每个地方的地名和评级。我已经把事情搞定了。我的意思是,我将数据保存到我的数据库,我也可以阅读它们。唯一的问题是,当我阅读它们时,我希望它们通过计算每个地方的平均值来加载我的tableviewcell。查看屏幕截图,如果您不了解某些内容,请让我编辑答案。

的TableView

TableView

火力地堡

Firebase

我的代码将数据加载到tableview

import UIKit
import FirebaseDatabase

class PlacesTableViewController: UITableViewController {

    //MARK: Properties
    @IBOutlet weak var placesTableView: UITableView!
    var dbRef:FIRDatabaseReference?
    var places = [Places]()
    private var loadedLabels = [String: String]()
    private var loadedRatings = [String: Int]()
    override func viewDidLoad()
    {
        super.viewDidLoad()

        dbRef = FIRDatabase.database().reference()

        // Loads data to cell.
        loadData()
    }
    override func numberOfSections(in tableView: UITableView) -> Int
    {
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {   
        //return the number of rows
        return places.count
    }
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
        // Table view cells are reused and should be dequeued using a cell identifier.


        guard let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as? PlacesTableViewCell  else {
            fatalError("The dequeued cell is not an instance of PlacesTableView Cell.")
        }

        let place = places[indexPath.row]

        cell.placeLabel.text = place.name
        cell.ratingControl.rating = place.rating

        return cell

    }
    private func loadData()
    {
        dbRef!.observe(.childAdded, with: {
            (placeSnapshot) in
            //print("Adding place \(placeSnapshot.key)...")

            let labels = placeSnapshot.childSnapshot(forPath: "placeLabel")

            for (key, label) in labels.value as! [String: String] {
                self.updatePlace(key, label: label)
            }
            let ratings = placeSnapshot.childSnapshot(forPath: "rating")
            for (key, rating) in ratings.value as! [String: Int] {
                self.updatePlace(key, rating: rating)
            }
        })
 }

    private func updatePlace(_ key: String, label: String? = nil, rating: Int? = nil)
    {
        if let label = label {
            loadedLabels[key] = label

        }
        if let rating = rating {
            loadedRatings[key] = rating
        }
        guard let label = loadedLabels[key], let rating = loadedRatings[key] else {
            return
        }
        if let place = Places(name: label, rating: rating) {
            places.append(place)
            placesTableView.reloadData()
        }
    }

}

快速放置

import UIKit

class Places {

    //MARK: Properties

    var name: String
    var rating: Int

    //MARK:Types

    struct PropertyKey {
        static let name = "name"
        static let rating = "rating"
    }

    //MARK: Initialization

    init?(name: String, rating: Int) {
        // Initialize stored properties.
        self.name = name
        self.rating = rating

        // Initialization should fail if there is no name or if the rating is negative.
        // The name must not be empty
        guard !name.isEmpty else {
            return nil
        }

        // The rating must be between 0 and 5 inclusively
        guard (rating >= 0) && (rating <= 5) else {
            return nil
        }

    }

}

2 个答案:

答案 0 :(得分:1)

据我所知,你希望你的应用程序有一个圆形的双倍而不是双倍。只需更改loadData()功能中的代码即可。你也会像你一样打电话给updatePlace()。请批准杰伊的答案,他写了代码。

private func loadData()
{
    dbRef!.observe(.childAdded, with: {
        (placeSnapshot) in
    let parentRef = self.dbRef?.child(placeSnapshot.key)
    let ratingRef = parentRef?.child("rating")
    ratingRef?.observe(.value, with: { snapshot in
        let count = snapshot.childrenCount
        var total: Double = 0.0
        for child in snapshot.children {
            let snap = child as! FIRDataSnapshot
            let val = snap.value as! Double
            total += val
        }
        let average = total/Double(count)
        print("Average for \(placeSnapshot.key) = \(Int(round(average)))")

        self.updatePlace("" , label: placeSnapshot.key, rating: Int(round(average)))

    })
    })

}

答案 1 :(得分:0)

这是一个非常详细的答案,但它会迭代评级节点的子节点并计算平均值

let parentRef = self.dbRef.child("Paradosiako - Panorama")

let ratingRef = parentRef.child("rating") // rating node
ratingRef.observe(.value, with: { snapshot in
    let count = snapshot.childrenCount
    var total: Double = 0.0
    for child in snapshot.children {
        let snap = child as! FIRDataSnapshot
        let val = snap.value as! Double
        total += val
    }
    let average = total/Double(count)
    print(average)
})

修改

要遍历所有地点并获得平均值,请输入代码

let placesRef = self.dbRef.child("akapnapp")

placesRef.observeSingleEvent(of: .value, with: { snapshot in

    for child in snapshot.children {

        let placeSnap = child as! FIRDataSnapshot
        let ratingsSnap = placeSnap.childSnapshot(forPath: "rating")

        let count = ratingsSnap.childrenCount
        var total: Double = 0.0
        for child in ratingsSnap.children {
            print(child)
            let snap = child as! FIRDataSnapshot
            let val = snap.value as! Double
            total += val
        }
        let average = total/Double(count)
        print(average)
    }
})

并显示代码的输出正常工作。我的子节点在问题中被命名为a,b,c而不是childByAutoId,但它可以以任何方式工作。

Snap (a) 3
Snap (b) 3
3.0
Snap (a) 2
Snap (b) 4
Snap (c) 5
3.67