在SwiftUI中读取带有子节点的Firebase实时数据库父节点时出现问题

时间:2020-09-16 01:44:51

标签: firebase firebase-realtime-database swiftui

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

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

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

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                                    
    ]

} 

在写入Firebase数据库时,由于/字符的使用崩溃,我该如何考虑子节点“海报”。

1 个答案:

答案 0 :(得分:2)

因此,基于我的两(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
        ]

    }

}

希望这对其他人有帮助。