我正在尝试根据已经创建的参考资料读写firebase



在此处输入图像描述

我有一个用户档案文件:

class UserProfile {
var uid:String
var email: String
var username:String
var photoURL:URL
init(uid:String, email:String, username:String, photoURL:URL) {
self.uid = uid
self.email = email
self.username = username
self.photoURL = photoURL
}
}

和后文件

class Post {
var id:String
var author:UserProfile
var text:String
var timestamp:Date
init(id:String, author:UserProfile, text:String,timestamp:Double) {
self.id = id
self.author = author
self.text = text
self.timestamp = Date(timeIntervalSince1970: timestamp / 1000)//divided by 1000 because firebase stores dates as milliseconds
}
} 

这是它在防火中心图像描述中显示的方式

因此,我要做的是重用这两个文件之前使用的引用(用户名、日期和url图像(。

这里是用于后文件的代码:

func oberseverRoomatePostFeed(){
let postRef = Database.database().reference().child("posts")
postRef.observe(.value, with: { snapshot in
var currentUserRoomatePose = [Post]()//temporary array
//array****************************
for child in snapshot.children {
if let roommateSnapshot = child as? DataSnapshot,
let dict = roommateSnapshot.value as? [String:Any],
let author = dict["author"] as? [String:Any],
let uid = author["uid"] as? String,
let photoURL = author["photoURL"] as? String,
let email = author["email"] as? String,
let username = author["username"] as? String,
let url = URL(string:photoURL),
let text = dict["text"] as? String,
let timeStamp = dict["timestamp"] as? Double{
let userP = UserProfile(uid: uid, email: email, username: username, photoURL: url)
let post = Post(id: roommateSnapshot.key, author: userP, text: text, timestamp: timeStamp)
currentUserRoomatePose.append(post)
}
}
self.posts = currentUserRoomatePose
self.tableView.reloadData()
})
}

这是我目前拥有的

class User: NSObject {
var name: String?
var currentUser: UserProfile
var currentPost: Post
init(dictionary: [String: Any], currentUser:UserProfile, currentPost:Post) {
self.name = dictionary["name"] as? String ?? ""
self.currentUser = currentUser
self.currentPost = currentPost
}
}
and
func fetchUser() {
let postRef = Database.database().reference().child("users")
postRef.observe(.value, with: { snapshot in
var currentUsers = [User]() // temp array
for child in snapshot.children {
if let userSnapshot = child as? DataSnapshot,
let dict = userSnapshot.value as? [String: Any],
let author = dict["author"] as? [String:Any],
let uid = author["uid"] as? String,
let photoURL = author["photoURL"] as? String,
let email = author["email"] as? String,
let username = author["username"] as? String,
let url = URL(string:photoURL),
let text = dict["text"] as? String,
let timeStamp = dict["timestamp"] as? Double {
let userP = UserProfile(uid: uid, email: email, username: username, photoURL: url)
let user = User(dictionary: [String : Any], currentUser: userP, currentPost: Post)
}
}
})
}

在此处输入图像描述

在此处输入图像描述

func checkLogin() {
if Auth.auth().currentUser?.uid == nil {
perform(#selector(backButton), with:nil, afterDelay:0)
} else {
let uid = Auth.auth().currentUser?.uid
Database.database().reference().child("Users/profile/").child(uid!).observeSingleEvent(of: .value, with: {(snapshot) in
if let dictionary = snapshot.value as? [String: AnyObject] {
self.navigationItem.title = dictionary["username"] as? String
}
}, withCancel: nil)
}
}

"Users" : {
"kFjK5Kcrk7dLCnd3fQOnhBcPQHz1" : {
"Email" : "cmhughes95@gmail.com",
"Full Name" : "Cameron Hughes",
"Google UID" : "112185374105612274429",
"provider" : "Google"
},
"profile" : {
"0Ef8GJch5PPZ8yE9jLSXAS7fVoK2" : {
"email" : "teclarke@aggies.ncat.edu",
"password" : "Tecl6013",
"photoURL" : "https://firebasestorage.googleapis.com/v0/b/aggie-wallet.appspot.com/o/user%2F0Ef8GJch5PPZ8yE9jLSXAS7fVoK2?alt=media&token=62827fc7-38ec-47ae-9972-c078ef1d486e",
"username" : "tec95"
},
"EsqtPIFUWQbXh0ItLWK0W3qxOdI2" : {
"email" : "teclarke@aggies.ncat.edu",
"password" :    "Tecl6013",
"photoURL" : "https://firebasestorage.googleapis.com/v0/b/aggie-wallet.appspot.com/o/user%2FEsqtPIFUWQbXh0ItLWK0W3qxOdI2?alt=media&token=40c82e6e-cc4d-4320-a0ab-434cc297567a",
"username" : "tyrek95"
},

现在,用户节点似乎出现了问题。您得到的uid与名为"profile"的节点处于同一级别,然后包含其他用户id。这行不通。uid应该都在同一级别。节点还包含不同的子节点,所以不清楚其目的是什么。这将是一个更好的结构:

users
uid_0
email: "test@test.com"
photoURL: "https://www.xxxxxx"
username: "tyrek95"
uid_1
email: "yipee@yipee.com"
photoURL: "https://www.yyyyy"
username: "someusername"

根据评论,你似乎试图在标题栏中输入一个用户名——如果这是任务的话,这个问题中有很多无关的代码。checkLogin和fetchUser函数不会被调用,虽然posts节点中的非规范化是可以的,但这是不必要的重复数据-你不需要复制电子邮件、照片URL,因为你知道uid,并且可以从用户节点获得

更好的结构是

posts
post_0
author: "uid_0"
text: "Hello, World"
timestamp: "some time"
post_1
author: "uid_1"
text: "What's happening?"
timestamp: "some time"

为了简单起见,让我们获取一篇帖子和相关用户,并打印出该用户在帖子中所说的内容。

let usersRef = self.ref.child("users")
let postsRef = self.ref.child("posts")
let postNum = "post_0"
let postToGetRef = postsRef.child(postNum)
postToGetRef.observeSingleEvent(of: .value, with: { postSnap in
let postDict = postSnap.value as! [String: Any]
let uid = postDict["author"] as! String
let postText = postDict["text"] as! String
let userToGetRef = usersRef.child(uid)
userToGetRef.observeSingleEvent(of: .value, with: { userSnap in
let userDict = userSnap.value as! [String: Any]
let userName = userDict["username"] as! String
print("(userName) said (postText)") //here you put the name in the title bar
})
})

输出为

tyrek95 said Hello, World

我为一篇文章做了这件事,但通过在posts节点上使用.value可以很容易地扩展它,它将读取所有的文章,然后在for中迭代它们。。循环以获取帖子信息和每个帖子的用户。

请注意,为了简洁起见,这里没有错误检查。

最新更新