0

I seem to have encountered an error with fetching data from Firebase.

My goal is to observe the jobs collection for any live updates, and the code below throws an error:

    func fetchJobs() {
    ref.child("jobposts").observe(.value) { (snapshot) in
        
        guard let dictionary = snapshot.value as? [String:AnyObject]  else { return}
        
        var job = JobData()
        
        job.title = (dictionary["title"] as! String) <-- Error Here
        job.company = (dictionary["company"] as! String)
        job.city = (dictionary["city"] as! String)
        job.salary = (dictionary["salary"] as! String)
        job.creator = (dictionary["creator"] as! String)
        
        self.jobs.append(job)
        
    }
}

Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value

The weird thing is, that this worked before and i'm not sure what happened, im just starting to understand SwiftUI after all :D

My Firebase database looks like this:

enter image description here

Thanks in advance for all the suggestions and possible solutions! <3

EDIT: Result from print(dictionary):

["-MkmZtye53DPpWb0-Wsn": {
city = 123;
company = 123;
creator = 8NHEiFMaCdSlEsBwuBw5oWrArOh1;
salary = Test;
title = 123;

}]

ToothFairy
  • 41
  • 9
  • One of your items *does not* have a `title` property. See [What does "Fatal error: Unexpectedly found nil while unwrapping an Optional value" mean?](https://stackoverflow.com/questions/32170456/what-does-fatal-error-unexpectedly-found-nil-while-unwrapping-an-optional-valu) – jnpdx Sep 29 '21 at 17:18
  • Seems weird, because everything seems to be correct. If i delete the listing, the app seems to be working, but when i add a new one it just crashes... – ToothFairy Sep 29 '21 at 17:23
  • Can you update the question with the result of `print(dictionary)`? – jnpdx Sep 29 '21 at 17:24
  • Your `title` is not a `String`. It's an `Int`. Thus, casting it to `String` fails. – jnpdx Sep 29 '21 at 17:26
  • Added the edit. P.S. the same happens with any value in title – ToothFairy Sep 29 '21 at 17:26
  • Note the types of all of the fields. You're using `Int` but trying to cast them all to `String`. This will always fail. It doesn't happen with "any value" in the title -- it happens when you don't use Strings. – jnpdx Sep 29 '21 at 17:27
  • Just tried creating a listing without any Integers, same problem persists. – ToothFairy Sep 29 '21 at 17:37
  • Can you print the result of the dictionary that doesn't have an `Int`s? – jnpdx Sep 29 '21 at 17:38
  • First test `dictionary["title"]` if it's nil, and then if it's a String, etc, for each one. But it maybe be more interesting to soft unwrap with `if let`... – Larme Sep 29 '21 at 17:39
  • ["-MkmZtye53DPpWb0-Wsn": { city = abc; company = abc; creator = 8NHEiFMaCdSlEsBwuBw5oWrArOh1; salary = Test; title = abc; }] – ToothFairy Sep 29 '21 at 17:39
  • And it still crashes on the same line? Sounds suspicious... – jnpdx Sep 29 '21 at 17:45
  • I know! The thing is, maybe i should use a different approach? I just need the app to be able to see the change in values in realtime – ToothFairy Sep 29 '21 at 17:46
  • 1
    What happens if you try to cast to `[String:String]` on your `guard` statement? Does it work or fail? – jnpdx Sep 29 '21 at 17:52
  • Seems to have worked, but the Jobs are not getting appended now, any idea why? – ToothFairy Sep 29 '21 at 17:57
  • It's getting to `jobs.append` but it's not appending? Where are you checking to see whether it's been appended? – jnpdx Sep 29 '21 at 18:50
  • I have a different view where the listing are being shown, its empty while using the [String:String] – ToothFairy Sep 29 '21 at 18:55
  • Unfortunately, that doesn't answer the question of where you are checking. Without seeing the code to see how they're being displayed, it's impossible to debug. – jnpdx Sep 29 '21 at 19:03
  • @jnpdx https://pastebin.com/NTz86Fry Thats the view i use to get the values The card template: https://pastebin.com/He9J0SdG – ToothFairy Sep 29 '21 at 19:06
  • That's not compilable code for me. You'd need to create a [mre]. Or, at least show `SessionStore`. – jnpdx Sep 29 '21 at 19:07
  • The SessionStore: https://pastebin.com/kQp2knuy – ToothFairy Sep 29 '21 at 19:11
  • 1
    Sorry -- too much to dig through and unrelated non-compilable code. I'd suggest trying to create a [mre] in a new question. – jnpdx Sep 29 '21 at 19:20

0 Answers0