在SwiftUI中读取带有子节点的Firebase实时数据库父节点时出现问题 [英] Issue reading Firebase realtime database parent node with children nodes in SwiftUI

查看:64
本文介绍了在SwiftUI中读取带有子节点的Firebase实时数据库父节点时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有以下Firebase Realtime数据库结构:

I have the following Firebase Realtime database structure:

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: { ... }
}

具有以下发布"结构:

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吗?!

when I run the code I always get the else portion of the statement executed with the error printed out?!

我认为问题在于以下代码:

I think the issue is around the following code:

let uID = value["poster/uid"] as? String,
let userName = value["poster/username"] as? String,

,因为它有一个子节点.我不确定如何在信息中访问该节点?

as this has a child node. I am not sure how I can access that node in information?

有人可以帮忙吗!

*******更新******

******* UPDATE ******

另一方面,我将如何写入当前拥有的数据库:

on the flip side, how would I write to the database currently I have :

func toAnyObject() -> Any {                  

    return [                          
        "poster/username": userName,             
        "poster/uid": uID,             
        "text": text,             
        "created": created,              
        "likes": likes,             
        "imageheight": iHeight,             
        "imageName": imageName                                    
    ]

} 

由于写入/ char崩溃,如何在写入Firebase DB时说明子节点海报".

how do i account for the child node 'poster' when writing to the firebase DB, as the use of the / char crashes.

推荐答案

因此,基于我的两(2)部分问题;最初只是一(1)个问题,但在Asperi为我提供了一个很好的答案以解决最初的阅读问题之后,写作部分变成了问题后变成了两(2).

So based on my two (2) part question; initially it was only one (1) question but it turned to two (2) after the writing portion became as issue after I was provided a great answer for the initial read question by Asperi.

我的答案结合了他的以上答案,然后我修改了其余内容,以满足对Firebase的读写要求.

My answer incorporated his above answer and then I modified the rest to meet the read and write to 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
        ]

    }

}

希望这对其他人有帮助.

Hope this helps others.

这篇关于在SwiftUI中读取带有子节点的Firebase实时数据库父节点时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆