Hi I am having difficulty in getting the values of an array inside a Firebase block
My code involves working out the numbers of multiple food items ordered (requestFoodItemArray[i]) and append them to an array (requestFoodNumberArray). I am able to get it successfully to run but then the values stored inside requestFoodNumberArray will disappear when I try to access it anywhere else except within the observe() block.
How can I "extend" the lifespan of the data stored in that particular array?
Thanks!
override func viewDidLoad() {
super.viewDidLoad()
populateCharts(completion: { requestNumberArray in
print(requestNumberArray)
})
}
func populateCharts (completion: @escaping (_ requestNumberArray: Array<Double>) -> Void) {
for i in 0 ..< requestFoodItemArray.count {
refRequest.child("\(requestFoodItemArray[i])").observe(.value, with: { snapshot in
var newItem: [FoodRequestItemList] = []
for child in snapshot.children {
if let snapshot = child as? DataSnapshot,
let requestItemList = FoodRequestItemList(snapshot: snapshot) {
newItem.append(requestItemList)
}
}
self.item = newItem
self.requestedItemArray = self.item.map({ $0.key })
var arrayCount : Double = Double(self.requestedItemArray.count)
var requestArray : [Double] = []
requestArray.append(arrayCount)
completion(requestArray)
}, withCancel: nil)
}