gpt4 book ai didi

json - 从 Firebase Swift 读取两个数据模型

转载 作者:行者123 更新时间:2023-11-30 10:35:32 25 4
gpt4 key购买 nike

我有两个模型:User 模型和图书Review 模型。我在评论中的用户名的类型为User,这样我就可以检索给定的评论以及创建该评论的用户。

我现在可以使用我的代码检索 Review,但不能检索 User 对象。我做错了什么?

用户类别

class User: NSObject {
var id: String?
var name: String?
var email: String?
var profileImageUrl: String?
init(dictionary: [String: AnyObject]) {
self.id = dictionary["id"] as? String
self.name = dictionary["name"] as? String
self.email = dictionary["email"] as? String
self.profileImageUrl = dictionary["profileImageUrl"] as? String
}
}

审核结构

struct Review {
let id: String
let bookName: String
let author: String
let review: String
let username: User
let createdAt: Date
init(id: String, bookName: String, author: String, review: String, username: User, timestamp: Double) {

self.id = id
self.bookName = bookName
self.author = author
self.review = review
self.username = username
self.createdAt = Date(timeIntervalSince1970: timestamp / 1000)
}
}

查看 Controller

 private func fetchReviews() {
let reviewRef = Database.database().reference().child("reviews")
let queryRef = reviewRef.queryOrdered(byChild: "timestamp").queryLimited(toLast: 5)

queryRef.observe(.value, with: { snapshot in

var temporaryReviews = [Review]()

for child in snapshot.children {
if let childSnapshot = child as? DataSnapshot,
let dictionary = childSnapshot.value as? [String:Any],
let user = dictionary["user"] as? [String:Any],
let id = user["id"] as? String,
let name = user["name"] as? String,
let profileImageUrl = user["profileImageUrl"] as? String,
let url = URL(string: profileImageUrl),
let bookName = dictionary["bookName"] as? String,
let author = dictionary["author"] as? String,
let review = dictionary["review"] as? String,
let timestamp = dictionary["timestamp"] as? Double {

let user = User(dictionary: <#T##[String : AnyObject]#> ??? // Cannot use a user object

let review = Review(id: childSnapshot.key, bookName: bookName, author: author, review: review, username: user, timestamp: timestamp)

DispatchQueue.main.async {
temporaryReviews.insert(review, at: 0)
self.reviews = temporaryReviews
self.tableView.reloadData()
}
}
}
})
}

如果我从循环中删除用户及其关联值,我会在 UI 中获取数据,但不会在 View 类中更新usernamephotoImageURL 。数据已存在于 Firebase 中。如何读取数据和用户?

最佳答案

尚不清楚该行的预期内容

let user = User(dictionary: <#T##[String : AnyObject]#> ???

并且也不知道为什么 User: NSObject 类是一个 NSObject,但这适用于 Swift 5。

class User { //using pure Swift, remove NSObject
var id: String?
var name: String?
var email: String?
var profileImageUrl: String?
init(dictionary: [String: Any]) { //change AnyObject to Any
self.id = dictionary["id"] as? String
self.name = dictionary["name"] as? String
self.email = dictionary["email"] as? String
self.profileImageUrl = dictionary["profileImageUrl"] as? String
}
}

然后,当在闭包中读取 Firebase 数据时,创建一个包含要填充用户类的项目的字典

let id = user["id"] as? String,
let name = user["name"] as? String,
let profileImageUrl = user["profileImageUrl"] as? String,
let url = URL(string: profileImageUrl),

let dict: [String: Any] = [
"id": id,
"name": name,
"email": email,
"profileImageUrl": profileImageUrl
]

let aUser = User(dictionary: dict)

或者您可以将快照传递给 User 类并让它从中获取所需的任何内容

class User {
var user_id = ""
var name = ""
var email = ""
var profileImageUrl = ""

init(withSnap: DataSnapshot) {
self.user_id = withSnap.childSnapshot(forPath: "id").value as? String ?? "No Id"
self.name = withSnap.childSnapshot(forPath: "name").value as? String ?? "No name"
self.email = withSnap.childSnapshot(forPath: "email").value as? String ?? "No email"
self.profileImageUrl = withSnap.childSnapshot(forPath: "profileImageUrl").value as? String ?? "No url"
}
}

然后

let aUser = User(withSnap: snapshot)

此外,在您的代码中,每次循环迭代都会重新加载 tableView,这可能会导致闪烁。最好的办法是将 self.tableView.reloadData() 移到 for 循环之外,这样它只执行一次。

关于json - 从 Firebase Swift 读取两个数据模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58143241/

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