How to read data from firebase and append it to an array - swift

I have a userImages collection and I am trying to read data from it and appending it to my imgUrls array.
This is my code where I read the data from the database and try appending it to my array. Unfortunately, I keep getting an error because the array is apparently empty.
override func viewDidLoad() {
var ref: DatabaseReference!
let userID = Auth.auth().currentUser?.uid //holds the current user uid
ref = Database.database().reference()
var imgUrls = [String]() //array to hold the image urls from the userImages collection
ref.child("userImages").child(userID!).observeSingleEvent(of: .value) { (snapshot) in //read from userImages collection only from the subcollection where the
guard let dict = snapshot.value as? [String: Any] else { return } //document Id equals the current user uid. Create a dictionary from the
//snapshot values
let values = dict.values //holds the values from the dictionary
for value in values { //for loop to go through each value from the dictionary
imgUrls.append((value as? String)!) //and append to the imgUrls array
}
}
testLabel.text = imgUrls[0] //I used this to test, but i get an error saying the array is empty
}
I posted a question before, but it was so convoluted I decided to delete it and repost it simpler.
Any help is much appreciated!

the reason you are not getting anything in your testLabel.text is because:
ref.child("userImages").child(userID!).observeSingleEvent(of: .value) { (snapshot) in //read from userImages collection only from the subcollection where the
...
}
is an asynchronous function called. That is it will be done sometimes in the future.
but your:
testLabel.text = imgUrls[0] //I used this to test, but i get an error saying the array is empty
is outside of this call. So the results in "imgUrls" are not available yet.
Put this line inside the function or wait until it has finished before using the results.

You try to use the array before the observeSingleEvent closure is executed. All observation calls to Firebase are asynchronous. This means that you test code is executed before the closure and the array is still empty.
ref.child("userImages").child(userID!).observeSingleEvent(of: .value) { snapshot in
// this closure is executed later
guard let dict = snapshot.value as? [String: Any] else { return }
let values = dict.values
for value in values {
imgUrls.append((value as? String)!)
}
// your test code should bee here
}
// this is executed before closure and the array is empty
testLabel.text = imgUrls[0]
For this reason you get the error. You need to add your test code into the closure.
However, there is another catch. Asynchronous calls (their closures) are executed on the background thread.
ref.child("userImages").child(userID!).observeSingleEvent(of: .value) { snapshot in
// this is executed on the background thread
}
However, all user interface calls must be executed on the main thread. Also wrap your test code by calling the main thread, otherwise you won't see the result in the user interface.
DispatchQueue.main.async {
// this code is executed on the main thread
// all UI code must be executed on the main thread
}
After editing, your code might look like this:
override func viewDidLoad() {
var ref: DatabaseReference!
let userID = Auth.auth().currentUser?.lid
ref = Database.database().reference()
var imgUrls = [String]()
ref.child("userImages").child(userID!).observeSingleEvent(of: .value) { snapshot in
guard let dict = snapshot.value as? [String: Any] else { return }
let values = dict.values
for value in values {
imgUrls.append((value as? String)!)
}
DispatchQueue.main.async {
testLabel.text = imgUrls[0]
}
}
}

Related

Firebase Database observeSingleEvent not working

I have a simple function in my code which gets the users name. However, when it comes to the line saying observeSingleEvent, it skips the whole closure and exits the function. Any ideas why?
func getUsersName() {
let userId = Auth.auth().currentUser?.uid
// It comes to the line below
ref?.child("users").child(userId!).child("name").observeSingleEvent(of: .value, with: { snapshot in
// It skips all of that
guard let value = snapshot.value as? NSDictionary else {
return
}
let name = value["name"] as? String ?? ""
print("Users name is: \(name)")
})
} // And comes here
I have it just like in the docs from firebase. The idea is that I have a pair: name: "users_name" and I want to get the users name and display it in a table view.

Access array outside of Firebase call

From the documentation I see that I can get some user data (which I'm already getting correctly), however, the way it's structured, it doesn't allow me to access the array outside of it, this is what I mean, I have a function:
func observe() {
let postsRef = Database.database().reference(withPath: "post")
struct test {
static var tempPosts = [Post]()
}
postsRef.observe(.value, with: { snapshot in
for child in snapshot.children {
if let childSnapshot = child as? DataSnapshot,
let data = childSnapshot.value as? [String:Any],
// let timestamp = data["timestamp"] as? Double,
let first_name = data["Author"] as? String,
let postTitle = data["title"] as? String,
let postDescription = data["description"] as? String,
let postUrl = data["postUrl"] as? String,
let postAddress = data["Address"] as? String,
let url = URL(string:postUrl)
{
// Convert timestamp to date
// let newDate = self.getDateFromTimeStamp(timestamp:timestamp)
// Store variables from DB into post
let post = Post(author: first_name, postTitle: postTitle, postDescription: postDescription, postUrl: url, postAddress: postAddress)
test.tempPosts.append(post)
}
}
self.posts = test.tempPosts
// HERE IT WORKS
print(test.tempPosts[0].postTitle , " 0")
self.tableView.reloadData()
})
// HERE IT DOESN'T WORK
print(test.tempPosts[0].postTitle , " 0")
}
and I'm trying to access the data where it says: // HERE IT DOESN'T WORK, how can I access that array outside of it? I need to call it later
The observe() method is asynchronous, so after you call postsRef.observe the code executed within that closure is run ONLY AFTER the application receives a response from Firebase, so there's a delay. All code after this call that's NOT stored within the closure will be executed immediately though.
So the .observe asynchronous function call is executed, and then the next line under // HERE IT DOESN'T WORK is executed immediately after. This is why this doesn't work because test.tempPosts doesn't contain any values until after the server response is received, and by that time, your print statement outside the closure has already run.
Check out this StackOverflow answer to get some more information about async vs sync.
Asynchronous vs synchronous execution, what does it really mean?
Also too, you may want to look into closures on Swift here.
If you want to access the value outside of the closure, you'll need to look into using a completion handler or a class property.
Edit:
Here's an example
func observe (finished: #escaping ([Post]) -> Void) {
// ALL YOUR CODE...
finished(test.tempPosts)
}
func getTempPosts () {
observe( (tempPosts) in
print(tempPosts)
}
}

How to store a data from a closure firebase in a local variable?

I'm trying to store the value of the completion block into a dictionary. But I don't know how to access and store it in a local dictionary variable.
var id = String()
var answeredDict = [String:[String]]()
var answeredDictUsers = [String:String]()
override func viewDidLoad() {
super.viewDidLoad()
for user in answeredDict.keys{
let ref = Database.database().reference(fromURL: "URL").child("users/\(user)")
ref.child("name").observeSingleEvent(of: .value) { (snap) in
guard let name = snap.value as? String else { return }
self.answeredDictUsers.updateValue(name, forKey: user)
}
}
print(answeredDictUsers)
}
the print(answeredDictUsers) gives [:] (empty dictionary)
Data is loaded from Firebase asynchronously. By the time your print(answeredDictUsers) runs, the code inside the callback hasn't run yet. And since self.answeredDictUsers.updateValue(name, forKey: user) hasn't run yet, the print sees an empty array. You can easily test this for yourself by placing breakpoints on both those lines and running the code in the debugger.
This means that any code that needs data from the database, must be (called from) inside the callback/completion handler that gets that data from the database.
A very simple example:
for user in answeredDict.keys{
let ref = Database.database().reference(fromURL: "URL").child("users/\(user)")
ref.child("name").observeSingleEvent(of: .value) { (snap) in
guard let name = snap.value as? String else { return }
self.answeredDictUsers.updateValue(name, forKey: user)
print(answeredDictUsers)
}
}
The above will print the dictionary each time it has loaded one of your users' data.
If you only want to print the dictionary once the data for all users has been loaded, you could for example keep a counter:
let count = 0
for user in answeredDict.keys{
let ref = Database.database().reference(fromURL: "URL").child("users/\(user)")
ref.child("name").observeSingleEvent(of: .value) { (snap) in
guard let name = snap.value as? String else { return }
self.answeredDictUsers.updateValue(name, forKey: user)
count = count + 1
if count == dict.count {
print(answeredDictUsers)
}
}
}

How do I get specific values from children in firebase using Swift 4?

My Firebase Database
More specifically, I have randomly generated children(Listings) and from those randomly generated Listings I would like to get the string value from the keys.
For example, if I wanted the Photo URL address, I would like to get the string value of the key "PhotoURL:".
Thank you in advance !
First you need to do is to import Firebase and then call a function from the Database class like so:
let ref = Database.database().reference().child("Listings")
You can call child recursively to go deeper into your tree
//.child("Listings").child("SomeListing").child("PhotoURL")
Then call observeSingleEvent to receive the values from firebase.
Your value is stored in the snapshot variable
ref.observeSingleEvent(of: .value, with: { (snapshot) in
guard let listingsDictionary = snapshot.value as? [String: Any] else { return }
listngsDictionary.forEach({ (key, value) in
// Here you can iterate through it
})
}) { (err) in
print("Failed to fetch following listings:", err)
}
Here is the code to get child values from Listings. 
var ListArr = [ListModel]()
let ref = Database.database().reference().child("Listings")
ref.observe(.childAdded, with: { (snapshot) in
print(snapshot)
guard let dictionary = snapshot.value as? [String : AnyObject] else {
return
}
let Obj = ListModel()
Obj.UID = snapshot.key
Obj.PhotoURL = dictionary["PhotoURL"] as? String
self.ListArr.append(Obj)
}, withCancel: nil)
}
You can set up the model class
class ListModel: NSObject {
var UID:String?
var PhotoURL:String?
}

Firebase paginating data returns same data?

Currently, I am initially loading the user's messages through :
func fetchMessages() {
if started == true {
let messageRef = Database.database().reference().child("messages").queryOrdered(byChild: "convoID").queryEqual(toValue: convoID).queryLimited(toLast: 10)
messageRef.observe(.childAdded) { (snapshot) in
if let value = snapshot.value as? NSDictionary {
let newMessage = message()
newMessage.messageText = value["content"] as? String
newMessage.sender = value["sender"] as? String
newMessage.messageID = snapshot.key
self.messageList.append(newMessage)
self.queryingStatus = true
self.messagesTableView.reloadData()
self.scrollToBottom()
}
}
}
}
Now, to minimize the data download, I decided to break the messages into chunks as such so that the user will download ten subsequent messages each time they pull up on the table view:
#objc func handleRefresh(_ refreshControl: UIRefreshControl) {
let lastIDDictionary = messageList[0]
let lastIDQueried = lastIDDictionary.messageID
let messageRefAddition = Database.database().reference().child("messages").queryOrdered(byChild: "convoID").queryLimited(toLast: 10).queryEnding(atValue: convoID, childKey: lastIDQueried!)
messageRefAddition.observeSingleEvent(of: .value) { (snapshot) in
for child in snapshot.children.allObjects as! [DataSnapshot] {
if let messageValue = child.value as? NSDictionary {
let newMessage = message()
newMessage.messageText = messageValue["content"] as? String
newMessage.sender = messageValue["sender"] as? String
newMessage.messageID = child.key
self.messageList.insert(newMessage, at: 0)
self.messagesTableView.reloadData()
}
}
}
refreshControl.endRefreshing()
}
The problem is, when I pull up on the table view, the first time it returns some new messages (I am not sure whether the order is even correct). However, when I pull on the table view again to refresh, it adds those same ten messages again. I printed the lastIDQueried in the refresh method, and after the initial load the ID remains the same even though I am accessing the first item in the array of dictionaries? Basically,when I refresh the table view, it is not querying the correct data and my pagination implementation does not seem to be working correctly.
Basically, the problem was that I was inserting the post in the wrong place in the array and the last item was still being added to the array which was always the same (as I ended on the value). As such, I added a counter that incremented each time a value was added. Then, I inserted the subsequent post at the counter value in the array then again incremented. Finally, if the message ID was equal to the current first message in array, I would not insert it.