I feel embarrassed asking questions here. Accordingly, I’ve tried 100 different things before resorting to asking you non-judgmental and generous wizards…
My model:
struct User: Identifiable {
var id = ""
var nickname = ""
var iLike = [""]
var likeMe = [""]
}
My content model:
class ContentModel: ObservableObject {
let db = Firestore.firestore()
@Published var myFriends = [User]()
@Published var usersWhoILike = [User]()
@Published var usersWhoLikeMe = [User]()
func getCurrentUserData() {
// This function works, with the exception of the call to self.getMyFriends() at the end.
let ref = db.collection("users").document(Auth.auth().currentUser!.uid)
ref.getDocument { [self] snapshot, error in
guard error == nil, snapshot != nil else {
return
}
let data = snapshot!.data()
let currentUser = UserService.shared.currentUser
currentUser.id = Auth.auth().currentUser!.uid
currentUser.nickname = data?["nickname"] as? String ?? ""
self.getUsersWhoLikeMe()
self.getUsersWhoILike()
// This is where I run into problems.
self.getMyFriends()
}
}
func getUsersWhoILike() {
// this function works (My previous problem with this function was not with the code, but the data. Thank, Chris, for pointing me in the right direction.)
let collection = db.collection("users").whereField("likeMe", arrayContains: Auth.auth().currentUser!.uid)
collection.getDocuments { snapshot, error in
if error == nil {
// declare a temp list of users
var userList = [User]()
for doc in snapshot!.documents {
let user = User(
id: doc["id"] as? String ?? "",
nickname: doc["nickname"] as? String ?? "",
iLike: doc["iLike"] as? [String] ?? [""],
likeMe: doc["likeMe"] as? [String] ?? [""]
)
userList.append(user)
}
DispatchQueue.main.async {
self.usersWhoILike = userList
}
}
}
}
func getUsersWhoLikeMe() {
// This function is essentially identical to the function above, except it queries different data. It works.
}
func getMyFriends() {
// This is where I am running into problems.
// self.usersWhoILike.count and self.usersWhoLikeMe.count both return 0, which I know isn't true as I am using this very data in my Views. (They should return 3 and 1, respectively, given the data.)
print("Users who I like: \(self.usersWhoILike.count)")
print("Users who like me: \(self.usersWhoLikeMe.count)")
// I would like to extract the ids from User into its own array, but can't figure out the syntax
// This is my best approximation, which doesn't work.
// var usersWhoILikeIDs = self.usersWhoILike[$0].id
// The above line of code not working, this is my less elegant workaround.
var usersWhoILikeIds = [""]
for user in usersWhoILike {
usersWhoILikeIds.append(user.id)
}
var usersWhoLikeMeIds = [""]
for user in usersWhoLikeMe {
usersWhoLikeMeIds.append(user.id)
}
// Compare the two arrays to generate a list of ids of the users who I like and like me.
let usersWhoAreMyFriends = usersWhoILikeIds.filter { usersWhoLikeMeIds.contains($0) }
}
My main issue is figuring out why self.usersWhoILike.count and self.usersWhoLikeMe.count are zero. Nothing else matters if I can’t access the data in these properties.
Figuring out who to copy an array out of a struct into another array is really a bonus question, as I think the code should work without it … but I sure would like to learn the right way to do this.
Any thoughts?