How to sort data in Firebase? - swift

I'm now able to sort posts and users by time.
My data structure looks like that:
posts
-postId
imageRatio:
imageUrl:
postText:
postTime:
uId:
users
-UserId
email:
profileImageURL:
radius:
uid:
username:
username_lowercase:
UPDATE
Now, I created a new class with all datas for the user and the posts:
class UserPostModel {
var post: PostModel?
var user: UserModel?
init(post: PostModel, user: UserModel) {
self.post = post
self.user = user
}
}
Declaration of my post array:
var postArray = [UserPostModel]()
Here, Im loading the datas into the new class:
self.observeRadius(completion: { (radius) in
let currentRadius = radius
// Üperprüfe, welche Posts im Umkreis erstellt wurden
let circleQuery = geoRef.query(at: location!, withRadius: Double(currentRadius)!)
circleQuery.observe(.keyEntered, with: { (postIds, location) in
self.observePost(withPostId: postIds, completion: { (posts) in
guard let userUid = posts.uid else { return }
self.observeUser(uid: userUid, completion: { (users) in
let postArray = UserPostModel(post: posts, user: users)
self.postArray.append(postArray)
print(postArray.post!.postText!, postArray.user!.username!)
self.postArray.sort(by: {$0.post!.secondsFrom1970! > $1.post!.secondsFrom1970!})
})
})
Here I'm loading the datas into the table view cells:
extension DiscoveryViewController: UITableViewDataSource {
// wie viele Zellen
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
print(postArray.count)
return postArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "DiscoveryCollectionViewCell", for: indexPath) as! DiscoveryCollectionViewCell
cell.user = postArray[indexPath.row]
cell.post = postArray[indexPath.row]
//cell.delegate = self
return cell
}
}
Thanks in advance for your help!

There's a lot of code in the question and sometimes, simpler is better. So let's take a Post class, load the posts, get the associated user name and store it in an array. Then when complete, sort and print the posts in reverse chronological order.
A class to hold the post data and the user name
class PostClass {
var post = ""
var timestamp: Int! //using an int for simplicity in this answer
var user_name = ""
init(aPost: String, aUserName: String, aTimestamp: Int) {
self.post = aPost
self.user_name = aUserName
self.timestamp = aTimestamp
}
}
Note that if we want to have have both post data and user data we could do this
class PostUserClass {
var post: PostClass()
var user: UserClass()
}
but we're keeping it simple for this answer.
Then an array to store the posts
var postArray = [PostClass]()
and finally the code to load in all of the posts, get the associated user name (or user object in a full example).
let postsRef = self.ref.child("posts")
let usersRef = self.ref.child("users")
postsRef.observeSingleEvent(of: .value, with: { snapshot in
let lastSnapIndex = snapshot.childrenCount
var index = 0
for child in snapshot.children {
let childSnap = child as! DataSnapshot
let uid = childSnap.childSnapshot(forPath: "uid").value as! String
let post = childSnap.childSnapshot(forPath: "post").value as! String
let timestamp = childSnap.childSnapshot(forPath: "timestamp").value as! Int
let thisUserRef = usersRef.child(uid)
thisUserRef.observeSingleEvent(of: .value, with: { userSnap in
index += 1
//for simplicity, I am grabbing only the user name from the user
// data. You could just as easily create a user object and
// populate it with user data and store that in PostClass
// that would tie a user to a post as in the PostUserClass shown above
let userName = userSnap.childSnapshot(forPath: "Name").value as! String
let aPost = PostClass(aPost: post, aUserName: userName, aTimestamp: timestamp)
self.postArray.append(aPost) //or use self.postUserArray to store
// PostUserClass objects in an array.
if index == lastSnapIndex {
self.sortArrayAndDisplay() //or reload your tableView
}
})
}
})
and then the little function to sort and print to console
func sortArrayAndDisplay() {
self.postArray.sort(by: {$0.timestamp > $1.timestamp})
for post in postArray {
print(post.user_name, post.post, post.timestamp)
}
}
Note that Firebase is asynchronous so before sorting/printing we need to know we are done loading in all of the data. This is handled via the lastSnapIndex and index. The index is only incremented once each user is loaded and when all of the posts and users have been loaded we then sort and print as the data is complete.
This example avoids messy callbacks and completion handlers which may be contributing to the issue in the question - this piece of code is suspect and probably should be avoided due to the asynchronous nature of Firebase; the sort function is going to be called well before all of the users are loaded.
UserApi.shared.observeUserToPost(uid: userUid) { (user) in
self.postUser.append(user)
}
self.postUser.sort(by: {$0.postDate! > $1.postDate!})
*please add error checking.

Related

Showing post data from logged in user only swift/xcode/firebase

Hey I have been following tutorials online to try and make a social media app and it works fine where all the posts made by users populates in one scene but in another scene I would like for the users who are currently logged in to be able to see their posts only instead of having to find their posts from a bunch of posts made by other users, below are my code samples:
Posts Code:
import Foundation
import UIKit
import Firebase
class Posts{
var id: String
var author: UserProfile
var text: String
var timestamp:Double
var createdAt:Date
init(id:String, author:UserProfile,text:String,timestamp:Double) {
self.id = id
self.author = author
self.text = text
self.timestamp = timestamp
self.createdAt = Date(timeIntervalSince1970: timestamp / 1000)
}
static func parse(_ key:String, _ data:[String:Any]) -> Posts? {
if let author = data["author"] as? [String:Any],
let uid = author["uid"] as? String,
let name = author["name"] as? String,
let photoURL = author["photoURL"] as? String,
let url = URL(string:photoURL),
let text = data["text"] as? String,
let timestamp = data["timestamp"] as? Double {
let userProfile = UserProfile(uid: uid, name: name, photoURL: url)
return Posts(id: key, author: userProfile, text: text, timestamp:timestamp)
func performDeepQuery() {
guard let user = Auth.auth().currentUser else { return }
let uid = user.uid
let ref = Database.database().reference().child("posts")
let query = ref.queryOrdered(byChild: "author/uid").queryEqual(toValue: uid)
query.observeSingleEvent(of: .value, with: { snapshot in
let allPosts = snapshot.children.allObjects as! [DataSnapshot]
for postSnap in allPosts {
let text = postSnap.childSnapshot(forPath: "text").value as? String ?? "No Text"
print(text)
//same as above
}
})
}
}
return nil }
}
func numberOfSections(in tableView: UITableView) -> Int {
return 2
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
switch section {
case 0:
return theposts.count
case 1:
return fetchingMore ? 1 : 0
default:
return 0
} }
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.section == 0 { let cell = HomeViewControllerScene.dequeueReusableCell(withIdentifier: "postCell", for: indexPath) as! Posting_TableViewCell
cell.set(theposts: theposts[indexPath.row])
return cell}
else {
let cell = HomeViewControllerScene.dequeueReusableCell(withIdentifier: "loadingCell", for: indexPath) as! LoadingCell
cell.spinner.startAnimating()
return cell
}
}
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
cellHeights[indexPath] = cell.frame.size.height
}
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return cellHeights[indexPath] ?? 72.0
}
Please someone help me out here I have been scratching my head for a while with this!!!
I believe the question is how to get the posts for a certain user. The structure looks good but no need to have a child node 'author' in each post so instead of this:
posts
post_id_0
author
author data
text: "Hello, World"
uid: "uid_0"
do this
posts
post_id_0
text: "Hello, World"
uid: "uid_0"
name: "usmaan"
photoURL:"https://firebasestorage..."
So now just query for this users posts (this is for Firestore, scroll down fo the RTDB solution)...
func getThisUsersPosts() {
let uid = "uid_0" //this users uid
self.db.collection("posts]").whereField("uid", isEqualTo: uid).getDocuments { (snapshot, error) in
if let err = error {
print(err.localizedDescription)
return
}
if let doc = snapshot?.documents {
for d in doc {
let text = d.get("text") as? String ?? "No Post Text"
print(text)
}
} else {
print("no posts found")
}
}
}
self.db points to my Firestore.
EDIT:
OP is using the Real Time Database so here's the code for that
func getThisUsersPosts() {
let uid = "uid_0"
let ref = self.ref.child("posts") //self.ref points to MY firebase.
let query = ref.queryOrdered(byChild: "uid").queryEqual(toValue: uid)
query.observeSingleEvent(of: .value, with: { snapshot in
let allPosts = snapshot.children.allObjects as! [DataSnapshot]
for postSnap in allPosts {
let text = postSnap.childSnapshot(forPath: "text").value as? String ?? "No Text"
print(text)
}
})
}
EDIT 2:
OP wants to keep their same structure.
To query for data that's two levels deep we use what's called Deep Query and will look something like this:
func performDeepQuery() {
let uid = "uid_0"
let ref = self.ref.child("posts")
let query = ref.queryOrdered(byChild: "author/uid").queryEqual(toValue: uid)
query.observeSingleEvent(of: .value, with: { snapshot in
let allPosts = snapshot.children.allObjects as! [DataSnapshot]
for postSnap in allPosts {
//populate your tableView datasource here
let post = PostClass()
post.postId = postSnap.key
post.name = postStap.childSnapshot("name").value as? String ?? "No Post Name"
post.text = postStap.childSnapshot("text").value as? String ?? "No Post Text"
self.postArray.append(post)
}
self.myTableView.reloadData()
})
}
which will perform a deep query on this structure
posts
post_0
author
uid: "uid_0"
name: "post 0 name"
text: "post 0 text"
The PostClass could be this
class PostClass {
var postId = ""
var name = ""
var text = ""
}
More Info:
To get the current users uid (which is covered in the getting started guide as well) and assuming you are authenticated (otherwise it will be nil)
guard let user = Auth.auth().currentUser else { return }
let uid = user.uid

Search for users that haven't been fetch yet

I have a quick question, I build a block of code that fetches all the users in the database for searching purposes. Preparing for thousands of users in the future, I programmed the method in a pagination way fetching a certain amount of users at a time. That is where the problem lies when I search for a user if the user hasn't been retrieve from the database yet through scrolling I can't search there profile. Does anyone have a suggestion on how I can tackle this?
Here is the code I use to fetch the users:
//create a method that will fetch a certain mount of users
func fetchUsers() {
if userCurrentKey == nil {
USER_REF.queryLimited(toLast: 21).observeSingleEvent(of: .value) { (snapshot) in
self.collectionView?.refreshControl?.endRefreshing()
guard let first = snapshot.children.allObjects.first as? DataSnapshot else { return }
guard let allObjects = snapshot.children.allObjects as? [DataSnapshot] else { return }
allObjects.forEach({ (snapshot) in
let uid = snapshot.key
Database.fetchUser(with: uid, completion: { (user) in
self.users.append(user)
self.collectionView?.reloadData()
})
})
self.userCurrentKey = first.key
}
} else {
USER_REF.queryOrderedByKey().queryEnding(atValue: userCurrentKey).queryLimited(toLast: 22).observeSingleEvent(of: .value, with: { (snapshot) in
guard let first = snapshot.children.allObjects.first as? DataSnapshot else { return }
guard let allObjects = snapshot.children.allObjects as? [DataSnapshot] else { return }
allObjects.forEach({ (snapshot) in
let uid = snapshot.key
if uid != self.userCurrentKey {
Database.fetchUser(with: uid, completion: { (user) in
self.users.append(user)
self.collectionView?.reloadData()
})
}
})
self.userCurrentKey = first.key
})
}
}
}
Here is the code I used to paginate the users:
//once the users pass a certain amount of cells paginate to fetch the next set of users
override func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
if users.count > 20 {
if indexPath.item == users.count - 1 {
print("Fetching...")
fetchUsers()
}
}
}
Lastly here is the code I used to filter through the users:
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
if searchText.isEmpty{
//if search text is empty fetch the users but display nothing
inSearchMode = false
filteredUsers = users
self.collectionView?.refreshControl = refreshController
} else {
//if search text is not empty search for the users
inSearchMode = true
self.collectionView?.refreshControl = nil
filteredUsers = self.users.filter { (user) -> Bool in
return user.username.lowercased().contains(searchText.lowercased())
}
}
//reload the table view data to update the displayed user
self.collectionView?.reloadData()
}
Thank you in advance!
The up front issue is you should not be in a situation where you have to load all users to search through them.
If you have a lot of data in the users node, loading that much data can overwhelm the device and secondly it's going to get very laggy for the user as the loaded data is iterated over.
Your best move is to denormalize your data and let the server do the heavy lifting by performing those queries and delivering only the data you need. Way faster and much easier to maintain. You can also add additional nodes to get to the data you want.
Looking at the code (I don't know your structure) it appears your goal is to have a searchfield where the users can type a username and the goal is to query for that username and return it lowercased.
A solution is to update your Firebase structure. Suppose it's like this
users
uid_0
userName: "MyCrazyUserNAME"
if you want to search, lowercased, add another node to your structure that's a lowercased version of the name
users
uid_0
userName: "MyCrazyUserNAME"
lowerCased: "mycrazyusername"
Then, perform a partial string query on the lowerCased node as the user types
func searchFor(thisPartialString: String) {
let userRef = self.ref.child("users")
let startString = thePartialString
let endString = thisPartialString + "\\uf8ff"
let query = ref.queryOrdered(byChild: "lowerCased")
.queryStarting(atValue: startString)
.queryEnding(atValue: endString")
query.observe....
}
The "\uf8ff" is a character at a very high code level in Unicode - because of that it encompasses all of the preceeding characters.

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.

Can't load my current user's posts from Firebase - Swift

I want to load my current user's posts from my Firebase Database.
This is my database's architecture:
I have already successfully loaded all the posts into a home view but when I tried to duplicate and use the same method here with the necessary changes and using the uid for the current user's posts, it's not showing anything... Have run out of ideas as to what the problem might be, could use some help.
Here's a screenshot from the ProfileUserPosts swift file:
//VARS
var postsuser = [ProfileUserPosts]()
#objc func observeUserPosts() {
let uid = Auth.auth().currentUser?.uid
let postsRef = Database.database().reference().child("posts").child("author")
postsRef.queryOrdered(byChild: "userid").queryEqual(toValue: uid!).observe(.value) { (snapshot) in
var tempPost = [ProfileUserPosts]()
for child in snapshot.children {
if let childSnapshot = child as? DataSnapshot {
let dict = childSnapshot.value as? [String: Any]
//Post Picture
let photoUrl = dict!["photoUrl"] as? String
let url = URL(string: photoUrl!)
//Info Post
let comments = dict!["comments"] as? String
let city = dict!["city"] as? String
let municipality = dict!["municipality"] as? String
let breed = dict!["breed"] as? String
let phoneuser = dict!["phone"] as? String
let postType = dict!["postType"] as? String
let petType = dict!["petType"] as? String
let gender = dict!["gender"] as? String
let timestampadoption = dict!["timestamp"] as? Double
let date = Date(timeIntervalSince1970: timestampadoption!/1000)
let post = ProfileUserPosts(breed: breed!, phone: phoneuser!, photoUrl: url!, city: city!, municipality: municipality!, petType: petType!, gender: gender!, timestamp: date, postType: postType!, comments: comments!)
tempPost.insert(post, at: 0)
}
DispatchQueue.main.async {
self.postsuser = tempPost
self.postsCollectionView.reloadData()
self.refresher.endRefreshing()
}
}
}
}
Here's where I'm loading the CollectionView with the numberOfItemsInSection and cellForItem
extension ProfileViewController: UICollectionViewDataSource,UICollectionViewDelegate {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return postsuser.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell: PostsCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "postsCell", for: indexPath) as! PostsCollectionViewCell
cell.set(post: postsuser[indexPath.row])
return cell
}
}
Well, my guess - based on your database structure - is that you're accessing an invalid path.
When you've referenced Database.database().reference().child("posts").child("author"), you're saying that posts have a direct child to author and that's not true, they've a direct child of a post id, which then fetching the respective post, you can get the author, so in this case, you would've to iterate through all the posts.
So, to fetch all the posts:
let postsRef = Database.database().reference().child("posts")
then you would have an Object (very important - this is not an array) of posts with ID's as keys, then you would iterate through them and fetch the respective author you want. I won't get into details of your implementation, but just to clarify what I'm saying:
let specificPostAuthor = postsRef.child("LOj1UZZgUnKa28xVtht").child("author")
would return the respective post author.

How do I check if 3 or more values are the same in my Firebase database?

I am currently trying to return all of the users in my table view that have 3 attributes that are equal to "art", "music" & "sports". The other users that have 3 attributes that are not equal to these 3 strings will not show in my table view. How do I achieve this? At the moment, I can only check if ONE attribute is equal to ONE string like in my example below. I have an image of my database tree below, along with the code that I currently have. Any appreciate any help. Thank you!
class User: NSObject {
var name: String?
var email: String?
var numberId: String?
var attribute1: String?
var attribute2: String?
var attribute3: String?
var password: String?
var profileImageUrl: String?
}
func fetchUser() {
FIRDatabase.database().reference().child("users").queryOrdered(byChild: "attribute1").queryEqual(toValue: "art").observe(.childAdded, with: { (snapshot) in
if let dictionary = snapshot.value as? [String: AnyObject] {
let user = User()
user.setValuesForKeys(dictionary)
self.users.append(user)
DispatchQueue.main.async(execute: {
self.tableView.reloadData()
})
}
}, withCancel: nil)
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! UserCell
let user = users[indexPath.row]
cell.textLabel?.text = user.name
cell .detailTextLabel?.text = user.email
if let profileImageUrl = user.profileImageUrl {
cell.profileImageView.loadImageUsingCachWithUrlString(urlString: profileImageUrl)
}
return cell
}
One (major, in my opinion) limitation of Firebase is its inability to filter by multiple keys at once. The general strategy for doing this is to filter by the key that you think will give you the smallest data set to work with, and filter by the remaining keys on the client side. So, for instance, if you think your current filter will give you the smallest set (or if they'll all give about the same), before you append the user, check:
if user.attribute2 == "music" && user.attribute3 == "sports" {
self.users.append(user)
}
You could also filter your list once you have all of your users. But the point is Firebase doesn't currently allow for this sort of filtering, so you'll need to do it client side.
self.users = self.users.filter({ $0.attribute2 == "music" && $0.attribute3 == "sports" })