无法从Firebase数据库Swift

时间:2017-02-07 05:56:37

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

我冒险将Firebase用于我的最终项目。我无法从Firebase数据库中检索我的数据。我不知道我错在哪里。请赐教。

主档案:

var ref: FIRDatabaseReference!
let user = FIRAuth.auth()?.currentUser

@IBOutlet var placename: UILabel!
@IBOutlet var placetype: UILabel!

var SiasuObj = [siasuObj]()
var PlaceObj = [placeObj]()

var planPriceLevel = ""

override func viewDidLoad() {
    super.viewDidLoad()

    fetchData()

    findPlaceToEat()
}

func fetchData(){

    //var planPriceLevel = ""
    ref = FIRDatabase.database().reference()

    //I can't pass through this part

ref.child("ios_entry").child(user!.uid).child("Budget").observeEventType(.ChildAdded, withBlock: {
        (snapshot) in

        if let dictionary = snapshot.value as? [String: AnyObject]{
            let SiasuObj = siasuObj()
            SiasuObj.setValuesForKeysWithDictionary(dictionary)
            self.SiasuObj.append(SiasuObj)
            print("Total budget")
            print(SiasuObj.total_budget, SiasuObj.total_day, SiasuObj.total_pax)
            SiasuObj.total_budget = dictionary ["total_budget"] as! String
            SiasuObj.total_day = dictionary["total_day"] as! String
            SiasuObj.total_pax = dictionary["total_pax"] as! String

            let  total_budget:Int32? = Int32(SiasuObj.total_budget);
            print(total_budget)
            let total_day:Int32? = Int32(SiasuObj.total_day);
            print(total_day)
            let total_pax:Int32? = Int32(SiasuObj.total_pax);
            print(total_pax)

            let budgetPerPax = (total_budget! / total_pax!) / (total_day! * 3);

            if(budgetPerPax < 50){
                self.planPriceLevel = "1";
                self.findPlaceToEat();
            }else if(budgetPerPax <= 150){
                self.planPriceLevel = "2";
                self.findPlaceToEat();
            }else if(budgetPerPax > 150){
                self.planPriceLevel = "3";
                self.findPlaceToEat();
            }
        }

        }, withCancelBlock: nil)
}

func findPlaceToEat(){
    print("inside findPlaceToEat()")

    print("Plan price level")
    print(planPriceLevel)
    //let pricelevel = String(planPriceLevel)
   // print("pricelevel")
    //print(pricelevel)

    ref.child("places_detail").child("price_level").child(planPriceLevel).childByAutoId().observeEventType(.ChildAdded, withBlock:{
        (snapshot) in
        if let dictionary = snapshot.value as? [String: AnyObject]{
            let PlaceObj = placeObj()
            //placeObj.setValuesForKeysWithDictionary(dictionary)

            //self.PlaceObj.append(PlaceObj)
            print("Whatever")
            print(PlaceObj.place_name, PlaceObj.place_type)
            PlaceObj.place_name = dictionary["place_name"] as! String
            PlaceObj.place_type = dictionary["place_type"] as! String
            PlaceObj.price_range = dictionary["price_range"] as! String
            PlaceObj.vegan_type = dictionary["vegan_type"] as! String
            PlaceObj.website = dictionary["website"] as! String



            self.placename.text = PlaceObj.place_name;


        }
    }, withCancelBlock: nil)
}

班级档案:

class placeObj: NSObject {

var place_name : String!
var place_type : String!
var price_range : String!
var vegan_type : String!
var website : String!
}

2 个答案:

答案 0 :(得分:2)

不使用.value,而是使用observeEventType:withBlock,因为它添加的子项只会在将新条目添加到firebase时获取数据。

要阅读路径中的数据并聆听更改,请使用observeSingleEventOfType:withBlock的{​​{1}}或FIRDatabaseReference方法观察FIRDataEventTypeValue个事件。

FIRDataEventTypeValue :读取并侦听路径中所有内容的更改。

  

Firebase文档说:您可以使用FIRDataEventTypeValue事件来读取给定路径中的数据,因为它   在事件发生时存在。此方法在触发时触发一次   每次收集数据时都会附加监听器,包括任何数据   孩子,变化。事件回调传递包含的快照   该位置的所有数据,包括子数据。如果没有数据,   返回的快照值为nil。

答案 1 :(得分:1)

更改此行

ref.child("places_detail").child("price_level").child(planPriceLevel).childByAutoId().observeEventType(.ChildAdded, withBlock:{
    (snapshot) in

到此:

ref.child("places_detail").child("price_level").child(planPriceLevel).childByAutoId().observeEventType(.Value, withBlock:{
    (snapshot) in

ObserveEvent类型 .ChildAdded 仅在添加新子项时为您提供信息。这就是使用 .Value

的原因

要检查的另一个重要事项是参考URL。检查Google-Info.plist中是否有正确的基本网址