在 SwiftUI 中读取带有子节点的 Firebase 实时数据库父节点的问题
Posted
技术标签:
【中文标题】在 SwiftUI 中读取带有子节点的 Firebase 实时数据库父节点的问题【英文标题】:Issue reading Firebase realtime database parent node with children nodes in SwiftUI 【发布时间】:2020-09-16 01:44:51 【问题描述】:我有以下 Firebase 实时数据库结构:
posts:
user1_uid:
-Kfm0p2EMcrpN8XcLOR5:
created_at: 1490119356.786182,
image_height: 374.9999999999999,
image_url: "ttps://firebasestorage.googleapis.com/v0/b/mak...",
like_count: 4,
poster:
uid: user1_uid,
username: "testuser"
,
-KgGuLttcC3PJbD8pWAT: ... ,
-KgLo0OrineV8l3_K9gK: ...
,
user2_uid: ... ,
user3_uid: ...
使用以下 'Post' 结构:
init?(snapshot: DataSnapshot)
guard
let value = snapshot.value as? [String: AnyObject],
let uID = value["poster/uid"] as? String,
let userName = value["poster/username"] as? String,
let text = value["text"] as? String,
let likes = value["likes"] as? Int,
let created = value["created"] as? String,
let iHeight = value["imageheight"] as? Double,
let imageName = value["imagename"] as? String
else
print("************************************************")
print("PostStore init?(snapshot: DataSnapshot) ERROR!!!")
print("************************************************")
return nil
self.id = snapshot.key
self.uID = uID!
self.userName = userName!
self.text = text!
self.created = created!
self.likes = likes!
self.iHeight = iHeight!
self.imageName = imageName!
当我运行代码时,我总是打印出else portion of the statement executed with the error
?!
我认为问题在于以下代码:
let uID = value["poster/uid"] as? String,
let userName = value["poster/username"] as? String,
因为它有一个子节点。我不确定如何访问该节点的信息?
有人可以帮忙吗!
*********更新******
另一方面,我将如何写入我目前拥有的数据库:
func toAnyObject() -> Any
return [
"poster/username": userName,
"poster/uid": uID,
"text": text,
"created": created,
"likes": likes,
"imageheight": iHeight,
"imageName": imageName
]
当使用/
char 崩溃时,我如何在写入 Firebase 数据库时考虑子节点“海报”。
【问题讨论】:
【参考方案1】:所以基于我的两 (2) 部分问题;最初它只是一 (1) 个问题,但在 Asperi 为我最初阅读的问题提供了一个很好的答案后,写作部分变成了问题后变成了两 (2) 个。
我的答案包含了他上面的答案,然后我修改了其余部分以满足对 Firebase 的读写。
struct Post: Identifiable
var id: String
var profile: Profile?
var uID: String
var userName: String
var text: String
var created: String
var likes: Int
var iHeight: Double
var imageName: String
init(id: String, profile: Profile, text: String, created: String, likes: Int, iHeight: Double, imageName: String )
self.id = id
self.profile = profile
self.userName = profile.userName
self.uID = profile.uID
self.text = text
self.created = created
self.likes = likes
self.iHeight = iHeight
self.imageName = imageName
init?(snapshot: DataSnapshot)
guard
let value = snapshot.value as? [String: AnyObject],
let poster = value["poster"] as? [String: AnyObject],
let uID = poster["uid"] as? String,
let userName = poster["username"] as? String,
let text = value["text"] as? String,
let likes = value["likes"] as? Int,
let created = value["created"] as? String,
let iHeight = value["imageheight"] as? Double,
let imageName = value["imagename"] as? String
else
return nil
self.id = snapshot.key
self.uID = uID
self.userName = userName
self.text = text
self.created = created
self.likes = likes
self.iHeight = iHeight
self.imageName = imageName
func toAnyObject() -> Any
return [
"poster": profile!.toAnyObject(),
"text": text,
"created": created,
"likes": likes,
"imageheight": iHeight,
"imagename": imageName
]
struct Profile: Codable
let uID: String
let userName: String
func toAnyObject() -> Any
return [
"uid": uID,
"username": userName
]
希望这对其他人有所帮助。
【讨论】:
以上是关于在 SwiftUI 中读取带有子节点的 Firebase 实时数据库父节点的问题的主要内容,如果未能解决你的问题,请参考以下文章
SwiftUI - 使用 ForEach 时,您应该在子视图中使用 `@State var` 还是 `let`