gpt4 book ai didi

ios - 不显示所有用户的用户数据,如何只显示喜欢该登录用户的人的数据?

转载 作者:行者123 更新时间:2023-11-29 05:34:35 25 4
gpt4 key购买 nike

数据库

"people" : {
"9RYUttVhCzXKs6H1XnZ63TZ8Dun2" : {
"Coordinates" : {
"latitude" : 15.78583,
"longitude" : -4.406417
},
"Education" : "CS",
"PhotoPosts" : "https://firebasestorage.googleapis.com/v0/b/daylike-2f938.appspot.com/o/images%2FPhotoPosts?alt=media&token=65961f53-47f0-40f5-b990-02fc05ef94ad",
"WhatIamConsideringBuying" : "Food",
"caption" : 1563880684210,
"likes" : 31,
"peopleWhoLike" : {
"-LkAU_XjMft_plwga-uw" : "NMNQYJ5z64fATK2MMs7m0ggHl0k2",
"-LkAVL6rqnlATHsnMXix" : "NMNQYJ5z64fATK2MMs7m0ggHl0k2",
"-LkAemFWYz4A1DkmTYrC" : "q3up6CyAe5PoEAooJwCclOGGcZd2",
"-LkAfAIvWE9lB-Os0IYK" : "K1eqsVZfKGgIf0sS1UZcPNxY62x1",
"-LkAgZNIsTlnuJzyOKO7" : "KRFfYoO8IwO1qiHknzP9wLCmUSp1",
"-LkB5k0o89n2dFS6bUzH" : "KRFfYoO8IwO1qiHknzP9wLCmUSp1",
"-LkB5k3USKvBZR62H6t8" : "KRFfYoO8IwO1qiHknzP9wLCmUSp1",
"-LkBHsFvOxtc5M_eLisY" : "KRFfYoO8IwO1qiHknzP9wLCmUSp1",
"-LkBQObubRDb1eY-L5ME" : "KRFfYoO8IwO1qiHknzP9wLCmUSp1",
"-LkBRuvEb0KX20Blrhpq" : "NMNQYJ5z64fATK2MMs7m0ggHl0k2",
"-LkBSSKYKEmtsT68w564" : "NMNQYJ5z64fATK2MMs7m0ggHl0k2",
"-LkGPtRpz_AwBhA_4Mhx" : "NMNQYJ5z64fATK2MMs7m0ggHl0k2",
"-LkGZA9t85w21219wPcZ" : "NMNQYJ5z64fATK2MMs7m0ggHl0k2",
"-LkG_SwHgKvQ-4hIGzVC" : "NMNQYJ5z64fATK2MMs7m0ggHl0k2",
"-LkG_ZLibLrGs2G62Pfr" : "NMNQYJ5z64fATK2MMs7m0ggHl0k2",
"-LkG_sDlzzMp3phVceTW" : "NMNQYJ5z64fATK2MMs7m0ggHl0k2",
"-LkGaKINLcD0j9_NtYa_" : "KRFfYoO8IwO1qiHknzP9wLCmUSp1",
"-LkGd0JcyfHwQXPUPK0U" : "KRFfYoO8IwO1qiHknzP9wLCmUSp1",
"-LkGdYuC1sGCUXpizo56" : "KRFfYoO8IwO1qiHknzP9wLCmUSp1",
"-LkGdeN3mG3vpWVx3Jci" : "KRFfYoO8IwO1qiHknzP9wLCmUSp1",
"-LkGdfjNR0dPPztJhUtr" : "KRFfYoO8IwO1qiHknzP9wLCmUSp1",
"-LkGdfyOF3Rg7fgh1Noo" : "KRFfYoO8IwO1qiHknzP9wLCmUSp1",
"-LkGdgJX7R-w8IB2ns50" : "KRFfYoO8IwO1qiHknzP9wLCmUSp1",
"-LkP99lTiKk5tXamW5yW" : "KRFfYoO8IwO1qiHknzP9wLCmUSp1",
"-LkP9YuvzkKIOmotFNSx" : "KRFfYoO8IwO1qiHknzP9wLCmUSp1",
"-LkQw0h80_jSCYZs8mNu" : "NMNQYJ5z64fATK2MMs7m0ggHl0k2",
"-LkT_bvTo8SlYTtJR12Q" : "KRFfYoO8IwO1qiHknzP9wLCmUSp1",
"-LkThUcjHQWLUvQk4xKQ" : "KRFfYoO8IwO1qiHknzP9wLCmUSp1",
"-LkYMJ4f6Cwy-OD0Ca-W" : "q3up6CyAe5PoEAooJwCclOGGcZd2",
"-Lk_1YzFt-h0UPSNpp82" : "KRFfYoO8IwO1qiHknzP9wLCmUSp1",
"-Lk_1_yh6dVzyCMv9ghS" : "KRFfYoO8IwO1qiHknzP9wLCmUSp1"
},
"postID" : "9RYUttVhCzXKs6H1XnZ63TZ8Dun2",
"users" : "nane19@aol.com"
},

以上是数据库。基本上所有喜欢这个人的用户都会显示他们的数据(照片等)。喜欢的人会在他们喜欢的人身上找到小时候的样子。

我在下面的代码中添加了这一行,仅显示所有用户的所有用户数据(照片等):“for thisUsersUid in thisUsersUid1”

 let thisUsersUid = Auth.auth().currentUser?.uid //Mr. Dunn's uid
let thisUsersUid1 = Database.database().reference().child("people").child(thisUsersUid!).child("peopleWhoLikedMe")

ref = Database.database().reference().child("people");

ref.observe(DataEventType.value, with: {snapshot in

if snapshot.childrenCount>0{

self.people.removeAll()

/////This line describes what I want to do, but I don't know how to write it in formal code : for thisUsersUid in thisUsersUid1 ////// {
for people in snapshot.children.allObjects as! [DataSnapshot] {
if people.key != thisUsersUid { //do not add this users info to the array
let peopleObject = people.value as? [String: AnyObject]

let peopleEducation = peopleObject?["Education"] as? String
let peopleWhatIamConsideringBuying = peopleObject?["WhatIamConsideringBuying"] as? String
let peoplePhotoPosts = peopleObject?["PhotoPosts"] as? String
let peopleimageDownloadURL = peopleObject?["imageDownloadURL"] as? String
let peoplepostID = peopleObject?["postID"] as? String
let peoplepeopleWhoLike = peopleObject?["peopleWhoLike"] as? String

let peopl = Userx(Education: peopleEducation, WhatIamConsideringBuying: peopleWhatIamConsideringBuying, PhotoPosts: peoplePhotoPosts, imageDownloadURL: peopleimageDownloadURL, postID: peoplepostID, peopleWhoLike: peoplepeopleWhoLike)
self.people.append(peopl)
}
self.table.reloadData()

这是使数据库引用 peopleWhoLike 的代码

     ref.child("people").child(self.postID).observeSingleEvent(of: .value, with:  {(snapshot) in

if let people = snapshot.value as? [String: AnyObject] {
let updateLikes: [String: Any] = ["peopleWhoLike/\(keyToPost)" : Auth.auth().currentUser!.uid]
ref.child("people").child(self.postID).updateChildValues(updateLikes, withCompletionBlock: { (error, reff) in



if error == nil {
ref.child("people").child(self.postID).observeSingleEvent(of: .value, with: { (snap) in
if let properties = snap.value as?[String: AnyObject]{
if let likes = properties["peopleWhoLike"] as? [String : AnyObject] {
let count = likes.count

let update = ["likes" : count]
ref.child("people").child(self.postID).updateChildValues(update)

最后,不会显示所有用户的数据,而是仅显示喜欢该登录用户的人的数据。

///这是我的代码,基于 Jay 的回答

func printPersonInfo(uid: String) {
let usersRef = Database.database().reference().child("people")
let thisUser = usersRef.child(uid)
thisUser.observeSingleEvent(of: .value, with: { snapshot in
let education = snapshot.childSnapshot(forPath: "Education").value as? String ?? "No Education"
let whatIamConsideringBuying = snapshot.childSnapshot(forPath: "WhatIamConsideringBuying").value as? String ?? "No WhatIamConsideringBuying"
print(education, whatIamConsideringBuying)

})
}

func loadPeopleWhoLikeMe() {
let uid = Auth.auth().currentUser!.uid
let thisUserRef = Database.database().reference().child("people").child(uid)
let myPeopleRef = thisUserRef.child("peopleWhoLike")
myPeopleRef.observeSingleEvent(of: .value, with: { snapshot in
let peopleArray = snapshot.children.allObjects as! [DataSnapshot]
for person in peopleArray {
let personUid = person.value as! String
self.printPersonInfo(uid: personUid)
}
})
}

最佳答案

假设您有两个 Firebase 节点,一个用于存储“人员”,另一个用于存储实际用户,这里有一些代码可以完成您想要的操作。

people
uid_0
likes: 45
peopleWhoLike:
random_0: uid_1
uid_1
like: 12
peopleWhoLike:
random_0: uid_2
random_1: uid_3

然后是存储所有用户信息的主用户节点

users
uid_0
email: "bill@mostexcellent.com"
name: "bill"
uid_1
email: "ziggy@stardust.com"
name: "ziggy"
uid_2
email: "leroy@jenkins.com"
name: "leroy"
uid_3
email: "ted@ mostexcellent.com"
name: "ted"

目标是读取当前用户的“人员”节点并获取喜欢该用户的人员的信息。假设登录用户是uid_1,我们将检索uid_2和uid_3的信息。

func loadPeopleWhoLikeMe() {
let thisUserRef = self.ref.child("people").child("uid_1")
let myPeopleRef = thisUserRef.child("peopleWhoLike")
myPeopleRef.observeSingleEvent(of: .value, with: { snapshot in
let peopleArray = snapshot.children.allObjects as! [DataSnapshot]
for person in peopleArray {
let personUid = person.value as! String
self.printPersonInfo(withUid: personUid)
}
})
}

上面的代码获取对 uid_1 的 peopleWhoLike 子节点的引用,加载所有子节点,然后迭代每个子节点以获得每个喜欢他们的人的相应 uid。当读取每个用户时,我们将用户 uid 传递给另一个函数来读取(在本例中)电子邮件和姓名,并将其打印到控制台。它也可以加载他们的图片或其他任务。

func printPersonInfo(withUid: String) {
let usersRef = self.ref.child("users")
let thisUser = usersRef.child(withUid)
thisUser.observeSingleEvent(of: .value, with: { snapshot in
let name = snapshot.childSnapshot(forPath: "name").value as? String ?? "No name"
let email = snapshot.childSnapshot(forPath: "email").value as? String ?? "No email"
print(name, email)
})
}

当针对上述结构运行此代码时,输​​出为

leroy leroy@jenkins.com
ted ted@mostexcellent.com

关于ios - 不显示所有用户的用户数据,如何只显示喜欢该登录用户的人的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57167009/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com