0

I am trying to get values from my firebasedb, on run, ref.observeSingleEvent(of: .value, with: { snapshot in causes fatal error unexpectedly found nil while unwrapping an Optional value. As im sure you can tell... I have no idea what im doing... Thank you in advance...

func geths() -> Int{
    var sch:Int = 0
    var nam:String = ""
    print("start geths")
    ref.observeSingleEvent(of: .value, with: { snapshot in
        if (snapshot.exists()){
            print("snapexist")
            if let snapval = snapshot.value as? [String:AnyObject]{
                let hs = snapval["hs"] as? String
                let name = snapval["name"] as? String
                self.hso = hs!
                self.nameo = name!
                nam = self.nameo
                if let myNumber = NumberFormatter().number(from: self.hso) {
                    let i = myNumber.intValue
                    sch = i
                }else{
                    sch = 0
                }
            }else{
                print("error")
            }
        }else{
            print("error")
        }
    })
    return sch
}

EDIT************ still dont work :( same errors

func geths() -> Int{
        var sch:Int = 0
        var nam:String = ""
        print("start geths")
        ref.observeSingleEvent(of: .value, with: { (snapshot) in
            if (snapshot.exists()){
                print("snapexist")
                let snapval = snapshot.value as? NSDictionary
                let hs = snapval?["hs"] as? String ?? ""
                let name = snapval?["name"] as? String ?? ""
                if (hs != nil){
                    self.hso = hs
                }else{
                    self.hso = "0"
                }
                if (name != nil){
                    self.nameo = name
                }else{
                    self.nameo = "bob"
                }
                nam = self.nameo
                if let myNumber = NumberFormatter().number(from: self.hso) {
                    let i = myNumber.intValue
                    sch = i
                }else{
                    sch = 0
                }
            }else{
                print("error")
            }
        })
        return sch
    }
Lukas
  • 3
  • 2
  • 1
    First of all, your print("snapexist") is wrong, because on this line it is just not true, check with snapshot.exists(). The fatal error happens, because you try to unwrap with ! a value that is nil. I think it is because hs or name is nil – Retterdesdialogs Apr 28 '17 at 14:41
  • Looks better now :) – Retterdesdialogs Apr 28 '17 at 14:53
  • @Retterdesdialogs what should I do now? – Lukas Apr 28 '17 at 15:09
  • @Likas, how you are initializing your firebase reference i.e. "ref" ? – prabhu Apr 28 '17 at 15:19
  • @prabhu var ref: FIRDatabaseReference! – Lukas Apr 28 '17 at 15:22
  • Right off the bat: you cannot return values from a Firebase call. You have to allow time for Firebase to retrieve it's data and then make that data available within the closure. Your calling code will continue to execute before firebase completes fetching it's data. See [this question](http://stackoverflow.com/questions/43027817/how-to-perform-an-action-only-after-data-are-downloaded-from-firebase/43029121#43029121) and my answer. Also, you didn't include enough code - how is *ref* defined? – Jay Apr 28 '17 at 17:28

2 Answers2

0

create firebase reference like this :

var ref: FIRDatabaseReference!

ref = FIRDatabase.database().reference()

for more details go though this link : https://firebase.google.com/docs/database/ios/read-and-write

prabhu
  • 684
  • 11
  • 27
0

You must initialize the ref variable, you was only declare it by

var ref: FIRDatabaseReference!

For example

var ref: FIRDatabaseReference! = FIRDatabase.database().reference(withPath: "hs")

You may read example about using Firebase here: https://www.raywenderlich.com/139322/firebase-tutorial-getting-started-2

Antigp
  • 865
  • 6
  • 12