Check Queried User IDs Against Added Field in SwiftUI Firestore
In this article, we will discuss how to query users in a line dating app whose IDs have not added a specific field for the currently logged-in user. We will use SwiftUI and Firestore as our primary tools. By the end of this article, you will have a solid understanding of how to implement this functionality in your app.
User Model
Our user model has several properties, including an ID, an array of blocked user IDs, and an array of user IDs that the current user has liked. Here's an example of what our user model might look like:
swift
struct User {
let id: String
let blocked: [String]
let liked: [String]
// other properties
}
Querying Users
To query users whose IDs have not added a specific field for the currently logged-in user, we need to perform a query in Firestore. Here's an example of how we might do this:
swift
func queryUsers() -> Query {
let currentUserId = Auth.auth().currentUser?.uid ?? ""
let query = Firestore.firestore().collection("users")
.whereField("liked", arrayNotContains: currentUserId)
.whereField("blocked", arrayDoesNotContain: currentUserId)
return query
}
In this example, we first get the current user's ID. We then create a query that selects all users whose "liked" array does not contain the current user's ID and whose "blocked" array does not contain the current user's ID. This query will return all users who have not added the current user to either their "liked" or "blocked" lists.
Using the Query
Once we have our query, we can use it to fetch the users we're interested in. Here's an example of how we might do this:
swift
func fetchUsers() {
let query = queryUsers()
query.getDocuments { (querySnapshot, error) in
if let error = error {
print("Error fetching users: \(error.localizedDescription)")
return
}
guard let snapshot = querySnapshot else { return }
snapshot.documents.forEach { document in
let user = try! document.data(as: User.self)
// do something with the user
}
}
}
In this example, we first get our query using the function we defined earlier. We then call the "getDocuments" method on the query to fetch the documents that match our query. Once we have the documents, we can convert them to User objects and do something with them (such as display them in a list).
Significance
Being able to query users who have not added the current user to their "liked" or "blocked" lists is an essential feature of any line dating app. It allows users to see who they might be interested in and who they haven't interacted with yet. By using SwiftUI and Firestore, we can easily implement this functionality in our app.
- We discussed how to query users whose IDs have not added a specific field for the currently logged-in user in a line dating app.
- We used SwiftUI and Firestore to implement this functionality.
- We defined a user model with several properties, including an ID, an array of blocked user IDs, and an array of user IDs that the current user has liked.
- We created a query that selects all users whose "liked" array does not contain the current user's ID and whose "blocked" array does not contain the current user's ID.
- We used the query to fetch the users we're interested in and convert them to User objects.
References