I’ve made a Firebase Realtime Database and it is set out like this.
I am making an attempt to get my SwiftUI app to obtain and show this data, however nothing I’ve tried up to now has labored. The info I am making an attempt to get particularly is all the things in Section2, Section2Copy, and some other object which might be youngsters of Section1.
I’ve tried to make an ObservableObject and sophistication to course of this knowledge, however the tutorials I’ve discovered have been very unhelpful up to now. Does anybody have any clue as to how to do that? Any assist can be actually appreciated.
That is what I’ve carried out up to now.
class FirebaseData: Encodable, Decodable {
let id: String = ""
let data: String = ""
}
extension Encodable{
var toDict: [String: Any]? {
guard let knowledge = attempt? JSONEncoder().encode(self) else {return nil}
return attempt? JSONSerialization.jsonObject(with: knowledge, choices: .allowFragments) as? [String: Any]
}}
class ViewModel: ObservableObject{
@Revealed var knowledge = [FirebaseData]()
var ref = Database.database().reference().baby("ATop/Section1/Section2")
func observelist(){
ref.observe(.worth) { father or mother in
guard let youngsters = father or mother.youngsters.allObjects as? [DataSnapshot] else {return}
self.knowledge = youngsters.compactMap({snapshot in
return attempt? snapshot.knowledge(as: FirebaseData.self)
})
}
}}