0

I am trying to get data from my database using Firebase. I am unsure how to unwrap and assign the variables. I am trying to have a title on the top of the screen based on the name imported, likes, and playing the video.

When I print the variable jdata it prints the Optional JSON from the firebase.

import Foundation
import SwiftUI
import AVKit
import Firebase


struct PlayerView: View {
    
    @State var likes: Int = 0
    @State var name: String = ""
    @State var url: String = ""
    @State let player = AVPlayer(url: URL(string: url))
    
    func liked() { likes += 1 }
    func update() {
        FirebaseApp.configure()
        let root = Database.database().reference()
        root.observeSingleEvent(of: .value, with: { (snapshot) in
            if let data = snapshot.value as? [String: Any] {
                let jdata = data["urls"] // Optional JSON
                print(jdata)
            }
        })
            
        
    }

    var body: some View {
        
        VStack{
            Text(name)
            VideoPlayer(player: player).padding()
        
            HStack {
                Button("Like", action: liked).padding()
                Text(String(likes)).padding()
            }
            HStack {
                Button("Prev"){}.padding()
                Button("Next"){}.padding()
            }
            Button(action: update) {
                Text("Update Database")
                
            }.padding()

        }
    }
}

My variable after it prints Optional({"-N25GwfJ9Cj7IlfkFNpL" = { likes = 0; name = "NameTest"; url = "Testing"; }; })

9
  • are you going to do your old trick, get the answer to the question, then delete the question, like you did last time? Commented May 16, 2022 at 2:39
  • Since you haven't included your schema, it's tough to say exactly how to parse it. If "urls" contains a single String, you could do if let jdata = data["urls"] as? String { self.url = jdata }. If it contains multiple URLs, as the name implies, you'd have to decide which item you want. Commented May 16, 2022 at 3:33
  • @workingdogsupportUkraine Hi again! I deleted the question because I received negative reviews on it. I assumed this platform is like Reddit and I did not want to have a bad question as the negative review was indicating. I thought I might have made a mistake posting the question since it was moved past the original question. Leading to making it harder to read for other people. That is why I deleted the previous question. Your help has been unbelievable, especially with understanding the language itself. Regardless, thank you for your time. Commented May 16, 2022 at 3:33
  • @jnpdx My JSON when I print it looks like this. Optional({"-N25GwfJ9Cj7IlfkFNpL" = { likes = 0; name = "Name "; url = "Jaded;kjashdfkljhwe;ifugweiufhwie"; }; }) I have tried using the data["urls"]["name"] syntax but this was not working for me. Commented May 16, 2022 at 3:36
  • Looks like you have a document keyed by id. You need to get the document out first, then query the properties on it. Commented May 16, 2022 at 3:42

1 Answer 1

1

based on the data you show, you could try this (untested) approach:

root.observeSingleEvent(of: .value, with: { snapshot in
    if let data = snapshot.value as? [String: Any],
        let jdata = data["urls"] as? [String: Any] {
        if let key = jdata.keys.first,
           let result = jdata[key] as? [String: Any] {
            print("-----> result: \(result)")
            if let likes = result["likes"] {
                print("-----> likes: \(likes)")
            }
        }
    }
  })
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.