Firebase Query Returning Nil When Given Parameter - swift

Im using this function to return the top 9 results in a data set in firebase. The array is then suppose to populate data fro a collection view. However when I go try to segue to the VC I get error "Index out of range".
var userHighlights = [Post]()
func loadUserHighlightPosts(_ userID: String, update: #escaping() -> Void) {
STORAGE_REF.child(userID).queryOrdered(byChild: "likes").queryLimited(toLast: 9).observeSingleEvent(of: .value, with: { (snapshot) in
for snap in snapshot.children.allObjects as! [DataSnapshot] {
let post = Post(postID: snap.key, postData: snap.value as! Dictionary<String,Any>)
self.userHighlights.insert(post, at: 0)
update()
}
if snapshot.childrenCount == 0 {
update()
}
})
}
However, when I run the same function for the current user, using a different reference, everything works fine.
var highlights = [Post]()
func loadCurrentUserHighlightPosts(_ update: #escaping() -> Void) {
currentUserHighlightHandle = CURRENT_USER_STORAGE_REF.queryOrdered(byChild: "likes").queryLimited(toLast: 9).observe(.value, with: { (snapshot) in
for snap in snapshot.children.allObjects as! [DataSnapshot] {
let post = Post(postID: snap.key, postData: snap.value as! Dictionary<String, Any>)
self.highlights.insert(post, at: 0)
update()
}
if snapshot.childrenCount == 0 {
update()
}
})
}
I am using prepareforsegue() to pass over the user's UID that is used in the functions parameter however the app continues to crash. Any help would be appreciated in advance, thanks!

Related

Retrieving Data From Firebase Auto Id's - Firebase Swift

I am wanting to capture all the values in my childByAutoId in firebase. Essentially, it stores all the items that a person has shortlisted. However, I do not seem to be capturing this, and I assume it is because I am not calling the snapshot correctly to factor the auto id's.
Database:
userID
-> Favourited
-> Auto Id
-> itemName: x
-> Auto Id
-> itemName: x
-> Auto Id
-> itemName: x
Code:
func retrieveItems() {
guard let userId = Auth.auth().currentUser?.uid else { return }
let ref = Database.database().reference().child("users/\(userId)/Favourited")
ref.observe(.value, with: { (snapshot) in
if snapshot.childrenCount>0 {
self.favUsers.removeAll()
for likes in snapshot.children.allObjects as! [DataSnapshot] {
let likesObject = likes.value as? [String: AnyObject]
let itemName = likesObject!["itemName"]
let likesList = Names(id: likes.key, itemName: itemName as! String?)
self.favUsers.append(likesList)
}
} else {
print("not yet")
}
})
self.favList.reloadData()
}
Could someone have a look and let me know what I may be doing wrong? Thank you!
This happens because Firebase loads data asynchronously, and right now you're calling reloadData before the self.favUsers.append(likesList) has ever run.
The call to reloadData needs to be inside the close/completion handler that is called when the data comes back from Firebase:
ref.observe(.value, with: { (snapshot) in
if snapshot.childrenCount>0 {
self.favUsers.removeAll()
for likes in snapshot.children.allObjects as! [DataSnapshot] {
let likesObject = likes.value as? [String: AnyObject]
let itemName = likesObject!["itemName"]
let likesList = Names(id: likes.key, itemName: itemName as! String?)
self.favUsers.append(likesList)
}
self.favList.reloadData() // 👈 Move this here
} else {
print("not yet")
}
})
I also recommend checking out some of these answers asynchronous data loading in Firebase.

Firebase Snapshot Isn't showing Data

Basically everything is working, except the showChild func is returning completion([]) because of the guard catData = Category(snapshot: catInfo). I am wondering why the guard let is returning completion. When I debug, catInfo does have 1 value as shown in my pic of database and I want to append catData.main to "cats". Below is code for the service method and Category model as well.
Firebase Database
static func showChild(completion: #escaping ([String]) -> Void) {
let ref = Database.database().reference().child("category").child(User.current.uid)
ref.observeSingleEvent(of: .value, with: { (snapshot) in
guard let snapshot = snapshot.children.allObjects as? [DataSnapshot] else {
return completion([])
}
var cats = [String]()
for catInfo in snapshot {
guard let catData = Category(snapshot: catInfo) else {
return completion([])
}
cats += catData.main
}
completion(cats)
})
}
import Foundation
import FirebaseDatabase.FIRDataSnapshot
class Category {
var key: String?
let main: [String]
init?(snapshot: DataSnapshot) {
guard !snapshot.key.isEmpty else {return nil}
if let dict = snapshot.value as? [String : Any]{
let main = dict["main"] as? [String]
self.key = snapshot.key
self.main = main ?? [""]
}
else{
return nil
}
}
}
The issue is pretty straightforward.
While your snapshot contains at least one node of data, it's not in a format that the Category init method understands. You're iterating over it's child nodes and in your screenshot, there's only one, with a key of 'main'
You are observing this node
fb_root
category
2ayHe...
and then you're iterating over it's child nodes which will be
main
0: Performance
so the key is 'main' and it's value is '0: Performance'
but your Category class is looking for a child node of 'main'
let main = dict["main"] as? [String]
There's not enough info to understand what will be contained in the rest of the structure so I can't tell you how to correct it, but at least you know what the problem is.
To clarify, this line
if let dict = snapshot.value as? [String : Any]
will make dict = [0: "Performance]

Firebase Data flickers on addition of new values

I am making a social app to which I am fetching some data and flushing it to the collection view. I am flushing the all the posts from firebase to the posts array. I am also fetching the user information that posted the specific image. Both the database are 2 different models. Following is my data model :
posts
|- <post_id>
|- caption
|- ImageURL
|- views
|- spot
|- spot_id
|- sender<user_id>
|- spotted(value)
|- timestamp
|- author(<user_id>)
users
|- <user_id>
|- name
Following is the way I am fetching the post data in collectionVC and storing all to posts array:
func initialiseAllPostsContent(){
FBDataservice.ds.REF_CURR_USER.child("connections/following").observe(.childAdded) { (snapshot) in
if let snapshot = snapshot.value as? String {
self.followerKeys.append(snapshot)
}
}
if uid != nil {
self.followerKeys.append(uid!)
}
FBDataservice.ds.REF_POSTS.queryOrdered(byChild: "timestamp").observe(.childAdded, with: { (snapshot) in
print("post key is ", snapshot.key)
if let postDict = snapshot.value as? Dictionary<String, Any> {
let key = snapshot.key
if let postAuthor = postDict["author"] as? String {
for user in self.followerKeys {
if postAuthor == user {
let post = Posts(postId: key, postData: postDict)
self.posts.append(post)
}
}
}
}
})
reloadCollectionViewData()
}
func reloadCollectionViewData() {
FBDataservice.ds.REF_POSTS.queryOrdered(byChild: "timestamp").observe(.value) { (snapshot) in
self.collectionView.reloadData()
}
}
//I am updating the views on the post after a method is successfull. As soon as this is called, and then if like is pressed, views flicker
func updateViews(postid: String, views: Int) {
let viewref = FBDataservice.ds.REF_POSTS.child(postid)
let newviews = views + 1
viewref.updateChildValues(["views":newviews])
}
// fetching the user data from the post data
func getAllPosts(pid: String, completion: #escaping ((String) -> ())) {
FBDataservice.ds.REF_POSTS.child(pid).observeSingleEvent(of: .value) { (snapshot) in
if let snapshot = snapshot.value as? Dictionary<String, Any> {
if let userid = snapshot["author"] as? String {
completion(userid)
}
}
}
}
func getpostAuthorData(authorId : String, completion: #escaping (User) -> ()) {
FBDataservice.ds.REF_USERS.child(authorId).observeSingleEvent(of: .value) { (snapshot) in
if let snapshot = snapshot.value as? Dictionary<String, Any> {
if let userCredential = snapshot["credentials"] as? Dictionary<String, Any> {
completion(User(userid: authorId, userData: userCredential))
}
}
}
}
This is how I am assigning data in my cellForItemAtIndexPath
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
self.posts.sort(by: { $0.timestamp < $1.timestamp})
let post = posts[indexPath.row]
if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as? SpotGroundCell {
cell.configureCellData(post: post)
getAllPosts(pid: post.postId) { (userid) in
self.getpostAuthorData(authorId: userid, completion: { (userdata) in
cell.configUserData(user: userdata)
})
}
return cell
} else {
return SpotGroundCell()
}
}
The code in my cell :
//Consider this as likes. I allow users to like multiple times. Once the model is loaded, it fetches all the spots according to the timestamp and then siplayer the most recent ones. Even this is doesn't display according to the current image and flickers. I replicate previous cell values even though I am refreshing the view.
var currentUserSpots = [Spot]() {
didSet {
self.currentUserSpots.sort(by: { $0.timestamp < $1.timestamp})
if !self.currentUserSpots.isEmpty {
self.emotionImage.image = UIImage(named: (self.currentUserSpots.first?.spotted)!)
self.emotionImage.alpha = 1
} else {
self.emotionImage.image = UIImage(named: "none")
self.emotionImage.alpha = 0.5
}
}
}
func configUserData(user: User) {
self.user = user
self.name.text = self.user.name
}
func configureCellData(post: Posts) {
print("Config is now called")
self.posts = post
self.caption.text = posts.caption
FBDataservice.ds.REF_POSTS.child(post.postId).child("spot").queryOrdered(byChild: "senderID").queryEqual(toValue: uid!).observeSingleEvent(of: .childAdded) { (snapshot) in
if let spotData = snapshot.value as? Dictionary<String, Any> {
let spot = Spot(id: snapshot.key, spotData: spotData)
if spot.spotted != nil {
self.currentUserSpots.append(spot)
}
}
}
}
Now whenever I am making a change or an event which updates the database(like updating a view). I see a flicker in the user object entities(such as name etc). That event also kills other processes and Notification Observers.
I scrapped the internet for the solutions, but by far just was able to find one, which doesn't solve my problem.
Any help will be greatly appreciated. I am really not sure where am I going wrong.
Whenever there is a change under REF_POSTS you right now:
delete all data from the view
re-add all data (including the change) to the view
Given that most changes will only affect one item in the list, you're making your view to N-1 more than is needed. This causes the flicker.
To solve this problem, you should listen to more granular information from the database. Instead of observing .value, add a listener for .childAdded. The completion block for this listener will be triggered whenever a new child is added, at which point you can just add the new child to your view.
FBDataservice.ds.REF_POSTS.queryOrdered(byChild: "timestamp").observe(.childAdded, with: { (snap) in
if let postDict = snap.value as? Dictionary<String, Any> {
let key = snap.key
if let postAuthor = postDict["author"] as? String {
for user in self.followerKeys {
if postAuthor == user {
let post = Posts(postId: key, postData: postDict)
self.posts.append(post)
}
}
}
}
})
As a bonus .childAdded also immediately fires for all existing child nodes, so you don't need the observer for .value anymore. I like keeping it myself though. As Firebase guarantees that it fires .value after all corresponding child* events, the .value event is a great moment to tell the view that all changes came in.
FBDataservice.ds.REF_POSTS.queryOrdered(byChild: "timestamp").observe(.value, with: { (snapshot) in
self.collectionView.reloadData()
})
You'll need a few more things for a complete implementation:
You should also observe .childChanged, .childMoved and childRemoved to handle those types of changes to the database.
Since a child may be added (or moved) anywhere in the list, you should actually use observe(_, andPreviousSiblingKey: ) to be able to put the item in the right spot in the list.

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
}

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

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()
}
}
}
})
}