IOS - Parsing objects from Firebase - Code running before download is completed - swift

I am trying to parse data from Firebase into an array of objects, and upon completion display the text from the first object in the array. However, I can't work out/find a solution to stop the code continuing before the download is complete. So it proceeds to update the user's completion to true, without displaying the text. This is the function as is, the downloading and appending to array works fine, but it skips to displayNextInSeries() before it's finished...
func parseSeries (ref: String) {
FIRDatabase.database().reference().child("library").child("series").child(ref).observeSingleEvent(of: .value, with: { (snapshot) in
if let snapshot = snapshot.children.allObjects as? [FIRDataSnapshot] {
for snap in snapshot {
print(snap)
if let pushSeriesDict = snap.value as? Dictionary<String, AnyObject> {
let key = snap.key
let push = PUSH_SERIES(pushKey: key, pushSeriesData: pushSeriesDict)
self.seriesArray.append(push)
}
}
}
})
print("\(ref) Series Count: \(self.seriesArray.count)")
displayNextInSeries()
}
The display next in series function sees the seriesArray.count = 0, so it doesn't update the texLbl:
func displayNextInSeries() {
if seriesProgress < seriesArray.count {
animateProgress(current: seriesProgress, total: seriesArray.count)
currentPUSH_SERIES = seriesArray[seriesProgress]
currentPUSH_SERIES.text = personaliseText(text: currentPUSH_SERIES.text)
textLbl.animateUpdate(currentPUSH_SERIES.text, oldText: previousText)
titleLbl.text = "\(currentPUSH_SERIES.title!)"
previousText = currentPUSH_SERIES.text
seriesProgress += 1
} else {
animateProgress(current: sessionProgress, total: sessionTarget)
titleLbl.text = ""
greetingPush()
seriesPlay = false
seriesProgress = 0
user.updateProgress(seriesName)
print(user.progress)
}
}
I may be doing something fundamentally wrong here. Your help is much needed and much appreciated! Thanks, Matt

The observeSingleOfEvent is an asynchronous call, calling the function inside the completionBlock will solve it,The problem is that your print function is being called even before observeSingleOfEvent is finished downloading data :-
func parseSeries (ref: String) {
FIRDatabase.database().reference().child("library").child("series").child(ref).observeSingleEvent(of: .value, with: { (snapshot) in
if let snapshot = snapshot.children.allObjects as? [FIRDataSnapshot] {
for snap in snapshot {
print(snap)
if let pushSeriesDict = snap.value as? Dictionary<String, AnyObject> {
let key = snap.key
let push = PUSH_SERIES(pushKey: key, pushSeriesData: pushSeriesDict)
self.seriesArray.append(push)
print("\(ref) Series Count: \(self.seriesArray.count)")
displayNextInSeries()
}
}
}
})
}

Related

How can I wait data from firebase?

I need to wait data from firebase otherwise my app gonna crash because of nil value. I searched about that and watch some videos and look at other questions but I can't see any firebase example actually. How can I wait data from firebase? How can I use dispatch or semaphore and which one is more efficient?
Here my sample code:
let uid = (Auth.auth().currentUser?.uid)!
let usrIndirimRef = Database.database().reference().child("users").child(uid).child("indirimSayisi")
usrIndirimRef.observe(.value) { (snap) in
let userIndir = snap.value as! Int
self.userIndirimSayisi = userIndir
let usrIndirimTipiRef = Database.database().reference().child("users").child(uid).child("indirimTipi")
usrIndirimTipiRef.observe(.value) { (snapshot) in
if userIndir < 1 {
self.userIndirim = "0"
self.indirimKontrol = false
self.tableView.reloadSections([1], with: .fade)
}else{
self.userIndirim = snapshot.value! as! String
self.tableView.reloadSections([1], with: .fade)
}
}
}
toplamHesapTutari()
Here toplamHesapTutari() function execute before the data userIndirim. Its can be solve maybe hold execution for that function but I can't decide that is correct way.
Don't wait, never wait, run the code you need to run inside the completion handler
let uid = (Auth.auth().currentUser?.uid)!
let usrIndirimRef = Database.database().reference().child("users").child(uid).child("indirimSayisi")
usrIndirimRef.observe(.value) { (snap) in
let userIndir = snap.value as! Int
self.userIndirimSayisi = userIndir
let usrIndirimTipiRef = Database.database().reference().child("users").child(uid).child("indirimTipi")
usrIndirimTipiRef.observe(.value) { (snapshot) in
if userIndir < 1 {
self.userIndirim = "0"
self.indirimKontrol = false
self.tableView.reloadSections([1], with: .fade)
}else{
self.userIndirim = snapshot.value! as! String
self.tableView.reloadSections([1], with: .fade)
}
self.toplamHesapTutari()
}
}
I had this problem before and I tried a lot of methods but still crash my app.
So I figure it out by putting the code that makes my app crash in a later function to spare some time for the essential data fully loads. I suggest you use try-catch and log to observe the error and prevent your app from keep crashing.
try {
if ((snapshot.child("photo").value as? String)==null) {
//if not load
} else {
//if load
}
}catch(ex: Exception){}
Here is some reference for you:
https://kotlinlang.org/docs/reference/exceptions.html

Firebase - Order child value from low to high

I want to order my tableView with values from Firebase from low to high. This is what I got so far:
Database.database().reference().child("Bier").queryOrdered(byChild: "Voor_prijs_int").queryStarting(atValue: 0).queryEnding(atValue: 10000).observeSingleEvent(of: .value) { snapshot in
print("test")
if let snapshots = snapshot.children.allObjects as? [DataSnapshot] {
print("test2")
for snap in snapshots {
print("test3")
if let postDict = snap.value as? Dictionary<String, AnyObject> {
print("test4")
let key = snap.key
let bier = BierModel.transformbier(dict: postDict, key: key)
self.bier_model.insert(bier, at: 0)
}
}
}
}
When I run this code it seems that it get stuck by print("test2"), because it never print
test3
I am not sure why this is, because I have got this code from a other project that worked.
Here is my Firebase structure:
Thanks
This code will sort the data based on the smallest value of variable to the largest:
self.bier_model.sort { $0.variable < $1.variable }

Showing just one image at the time from database (Swift and Firebase)

First of all, I am new in this, so please do not make fun of me :)
Basically, I am trying to show and Image of a product but if the client refuses the product this item will not appear on his account. That is why I am creating another table Rejected (setAcceptedOrRejected) where I put the ID of the product and the Id of the client so I wont see the item he rejected before.
What I tried here it was to get the List (Good) with all the items and the (Bad) with the rejected items. Then compare it to display the picture of the item again.
My problem is that I want to show only 1 picture at the time, if the client refuses then it will show the next one and so on but it wont show that picture again.
I hope you can really help me with this one.
Thank you
func updateImage() {
createListProductsBad ()
var badnot = ""
for bad2 in listProductsBad{
badnot = bad2
}
Database.database().reference().child("Products").child(bad2)queryOrderedByKey().observe(.childAdded, with: { snapshot in
let userInfo = snapshot.value as! NSDictionary
let storageRef = Storage.storage().reference(forURL: profileUrl)
storageRef.downloadURL(completion: { (url, error) in
do {
let data = try Data(contentsOf: url!)
let image = UIImage(data: data as Data)
self.productPhoto.image = image
}
catch _ {
print("error")
}
})
})
}
func setAcceptedOrRejected() {
let notThankyou = [ "ProductID": ProductId,
"UserID": userUID
] as [String : Any]
self.storyboard?.instantiateViewController(withIdentifier: "Home")
self.refProducts.child("Rejected").childByAutoId().setValue(notThankyou)
}
func createListProductsGood () {
Database.database().reference().child("Products").queryOrderedByKey().observe(.childAdded, with: { snapshot in
if !snapshot.exists() { return }
let userInfo = snapshot.value as! NSDictionary
let goodID = String(snapshot.key)
for prod in self.listProductsBad{
if (prod == goodID){
print("Not good **********************")
}else{
if (goodID != "" ){
self.listProductsGood.append(prod)
}
}
}
})
}
func createListProductsBad () {
Database.database().reference().child("Rejected").queryOrderedByKey().observe(.childAdded, with: { snapshot in
let userInfo = snapshot.value as! NSDictionary
let currentID = userInfo["UserID"] as! String
let badProduct = userInfo["ProductID"] as! String
if (self.userUID == currentID ){
self.listProductsBad.append(badProduct)
}
})
}
}
//These can also be swift's dictionaries, [String: AnyObject] or possibility arrays if done correctly. All depends on your style of programming - I prefer NSDictionaries just because.
let availableKeys: NSMutableDictionary = [:]
let rejectedKeys: NSMutableDictionary = [:]
//Might be a better way for you. Depends on what you are looking for.
func sortItems2() -> NSMutableDictionary{
for rejKey in rejectedKeys.allKeys{
//Removes if the rejected key is found in the available ones
availableKeys.remove(rejKey)
}
return availableKeys
}

How to work with Firebase Asynchronously? Database reading giving odd results

I have written the following function to search through my Firebase database and I have also looked into using debug statements and tested with breakpoints to see this function is pulling the correct data and it is. But when I return the array at the end, the array is empty. As far as I understand this is due to the asynchronous nature of firebase. The function is getting to the end before the data is being added to the array. How do I fix this so it can work as intended, I want to return an array of items which I can then use for other functions.
static func SearchPostsByTags(tags: [String]) -> [Post]{
var result = [Post]()
let dbref = FIRDatabase.database().reference().child("posts")
dbref.observeSingleEvent(of: .value, with: { snap in
let comps = snap.value as! [String : AnyObject]
for(_, value) in comps {
let rawTags = value["tags"] as? NSArray
let compTags = rawTags as? [String]
if compTags != nil {
for cTag in compTags! {
for tag in tags {
if (tag == cTag) {
let foundPost = Post()
foundPost.postID = value["postID"] as! String
foundPost.title = value["title"] as! String
result.append(foundPost)
}
}
}
}
}
})
return result
}
}
You are returning your array before the async call ends. You should fill your array inside the async call and call then another method, which provides the results.
static func SearchPostsByTags(tags: [String]) {
let dbref = FIRDatabase.database().reference().child("posts")
dbref.observeSingleEvent(of: .value, with: { snap in
let comps = snap.value as! [String : AnyObject]
var result = [Post]()
for(_, value) in comps {
let rawTags = value["tags"] as? NSArray
let compTags = rawTags as? [String]
if compTags != nil {
for cTag in compTags! {
for tag in tags {
if (tag == cTag) {
let foundPost = Post()
foundPost.postID = value["postID"] as! String
foundPost.title = value["title"] as! String
result.append(foundPost)
}
}
}
}
}
// Call some func to deliver the finished result array
// You can also work with completion handlers - if you want to try have a look at callbacks / completion handler section of apples documentation
provideTheFinishedArr(result)
})
}

Retrieving data from Firebase does not work

I have this code below in order to retrieve a list of restaurants and their data. However, it's not storing the data, and every time I try to return the array it returns nil. But if I print it, prints the data. Any suggestions?
func getRestaurants()-> Array<Restaurant>{
var baruri = [Restaurant]()
dataBaseRef.child("AthensRestaurants/Restaurants").observe(.value, with: { (snapshot) in
if let snapshot = snapshot.children.allObjects as? [FIRDataSnapshot] {
for snap in snapshot {
print(snap)
if let restaurantData = snap.value as? Dictionary<String, AnyObject> {
let restaurant = Restaurant(restaurantData: restaurantData)
baruri.append(restaurant)
print(baruri)
}
}
}
})
return baruri
}
The firebase observe is an asynchronous callback function, so it will run after it is finished. In other words, your return baruri will always runs before the value got back. You can use completion handler to get the value you want.
var restaurants = [Restaurant]()
func getRestaurants(completion: #escaping (Array<Restaurant>) -> Void){
var baruri = [Restaurant]()
dataBaseRef.child("AthensRestaurants/Restaurants").observe(.value, with: { (snapshot) in
if let snapshot = snapshot.children.allObjects as? [FIRDataSnapshot] {
for snap in snapshot {
print(snap)
if let restaurantData = snap.value as? Dictionary<String, AnyObject> {
let restaurant = Restaurant(restaurantData: restaurantData)
baruri.append(restaurant)
print(baruri)
completion(baruri)
}
}
}
})
}
// Call this function with call back
getRestaurants { restaurants in
self.restaurants = restaurants
}