Reputation: 87
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!
}
Upvotes: 1
Views: 789
Reputation: 73
Instead of using .observeEventType(.ChildAdded)
, use .value
as it child added will only fetch data when new entry has been added to firebase.
To read data at a path and listen for changes, use the observeEventType:withBlock
or observeSingleEventOfType:withBlock
methods of FIRDatabaseReference
to observe FIRDataEventTypeValue
events.
FIRDataEventTypeValue: Read and listen for changes to the entire contents of a path.
Firebase documentation says: You can use the
FIRDataEventTypeValue
event to read the data at a given path, as it exists at the time of the event. This method is triggered once when the listener is attached and again every time the data, including any children, changes. The event callback is passed a snapshot containing all data at that location, including child data. If there is no data, the value of the snapshot returned is nil.
Upvotes: 2
Reputation: 4174
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
Upvotes: 1