I am trying to get a specific value from a child and then get the rest from other child. This is how my Database looks like. I want to get the value from the key longDescription
put of the Bienmesabe
.
func getCakeDescription (from category: String, subcategory: String, handler: @escaping (_ cakeDescription: [GeneralInfo]) -> ()) {
var cakeDescriptionArray = [GeneralInfo]()
let trimCategory = category.trimmingCharacters(in: .whitespaces)
let trimSubcategory = subcategory.replacingOccurrences(of: " ", with: "")
REF_CAKES.child(trimCategory).child(trimSubcategory).observeSingleEvent(of: .value) { (returnedDescriptionSnapshot) in
guard let returnedDescriptionSnapshot = returnedDescriptionSnapshot.children.allObjects as? [DataSnapshot] else {return}
for cakesDesc in returnedDescriptionSnapshot{
let cakeLongDescription = cakesDesc.childSnapshot(forPath: "longDescription").value as! String
self.REF_CAKES.child("General").observeSingleEvent(of: .value) { (returnedGeneralSnapshot) in
guard let returnedGeneralSnapshot = returnedGeneralSnapshot.children.allObjects as? [DataSnapshot] else {return}
for generalInfo in returnedGeneralSnapshot{
let titleDelivery = generalInfo.childSnapshot(forPath: "TitleDelivery").value as! String
let orderAndDelivery = generalInfo.childSnapshot(forPath: "OrderAndDelivery").value as! String
let titleSizeAndPrice = generalInfo.childSnapshot(forPath: "TitleSizeAndPrice").value as! String
let sizeAndPrice = generalInfo.childSnapshot(forPath: "SizeAndPrice").value as! String
let titlePromo = generalInfo.childSnapshot(forPath: "TitlePromo").value as! String
let promotions = generalInfo.childSnapshot(forPath: "Promotions").value as! String
let cakesInfo = GeneralInfo(titleDevilery: titleDelivery, titleSizeAndPrice: orderAndDelivery, titlePromo: titleSizeAndPrice, sizeAndDelivery: sizeAndPrice, orderAndDelivery: titlePromo, promotions: promotions, cakeDescription: cakeLongDescription)
cakeDescriptionArray.append(cakesInfo)
}
}
}
handler(cakeDescriptionArray)
}
}
SOLVED
I had a read through the documentation and did some test on how it should work and I came out to this answer. What I needed to do was changing where I was putting my handler. Also I changed my DB structure to be easier to pull. A simple code on how to extract a specific value in your database is below.
func getDescription (from category:String, and subcategory:String, handler: @escaping (_ cakeDescription: String)->()){
let trimSubcategory = subcategory.replacingOccurrences(of: " ", with: "")
REF_CAKES.child(category).child(trimSubcategory).observeSingleEvent(of: .value) { (cakeDescSnapshot) in
guard let cakeDescSnapshot = cakeDescSnapshot.children.allObjects as? [DataSnapshot] else { return }
for desc in cakeDescSnapshot{
if desc.key == "longDescription"{
handler(desc.value as! String)
}
}
}
}
There are other ways, you could also use the query method. An example below
func getCakeDescription (from category: String, subcategory: String, handler: @escaping (_ cakeDescription: String) -> ()) {
let trimSubcategory = subcategory.replacingOccurrences(of: " ", with: "")
REF_CAKES.child(category).child(trimSubcategory).queryOrdered(byChild: "longDescription").observeSingleEvent(of: .childAdded) { (snapshot) in
handler(snapshot.value as! String)
})
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.