无法从Firebase数据库Swift中的JSON检索数据(Unable to retrieve data from JSON in Firebase Database Swift)

编程入门 行业动态 更新时间:2024-10-22 23:08:28
无法从Firebase数据库Swift中的JSON检索数据(Unable to retrieve data from JSON in Firebase Database Swift)

我冒险将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! }

I took a risk to use Firebase for my final project. I am not able to retrieve my data from Firebase Database. I have no idea where I'm wrong. Please enlighten me.

Main file:

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 file:

class placeObj: NSObject { var place_name : String! var place_type : String! var price_range : String! var vegan_type : String! var website : String! }

最满意答案

改变这一行

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中是否有正确的基本URL

Change this line

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

to this:

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

ObserveEvent type is .ChildAdded only gives you information when new child is added. That's why use .Value

One more important thing to check is reference URL. Check you have correct base URL in Google-Info.plist or not

更多推荐

本文发布于:2023-07-16 07:37:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1125487.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据库   数据   JSON   Swift   Firebase

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!