2

I am making an app that stores photos for the user and I am trying to get some values from the Firebase database using .observeSingleEvent(). I used the same block of code earlier and successfully but now I am getting the error, "URL scheme must be one of gs://, http://, or https://". Here is my code:

let databaseRef = Database.database().reference()
let coverPhotosRef = databaseRef.child("images_" + (Auth.auth().currentUser?.uid)! + "_links/CoverPhotos")

print("Reference: " + String(describing: coverPhotosRef))

//error occurs here when code calls observeSingleEvent
coverPhotosRef.observeSingleEvent(of: .value, with: { (snapshot) in
    for rest in snapshot.children.allObjects as! [DataSnapshot] {
        //do stuff
    }
})

I can't figure out why it doesn't work. I printed coverPhotosRef and I get the correct URL in https:// format which leads to my database. I haven't gotten this error before even though I used the same exact code elsewhere. Any help is appreciated. Thanks.

Hugo Zhang
  • 67
  • 1
  • 9
  • did you print `snapshot` to make sure it's returning what you want? – Thomas Wang Nov 12 '17 at 01:33
  • The code actually crashes before it reaches the for loop. The line "coverPhotosRef.observeSingleEvent(of: .value, with: { (snapshot) in" causes it to crash although I am not sure why because I checked the URL which is correct. – Hugo Zhang Nov 12 '17 at 01:35
  • it could be that you're using the bang operator when trying to get the current user. try `guard let uid = Auth.auth().currentUser?.uid else { return }`? – Thomas Wang Nov 12 '17 at 01:36
  • What should I put after the return statement? guard let uid = Auth.auth().currentUser?.uid else { return nil } let coverPhotosRef = databaseRef.child("images_" + uid + "_links") returns an error – Hugo Zhang Nov 12 '17 at 01:39
  • 1
    You don't need to put anything after the return. The guard let statement just prevents the app from crashing if it gets `nil`. It just will exit the entire function call. – Thomas Wang Nov 12 '17 at 01:43
  • Don't return `nil` - that's the whole point of the return statement – Thomas Wang Nov 12 '17 at 01:44
  • When nothing follows the return statement like how you suggested, I get the compile error "Non-void function should return a value" so I added nil to see if that would fix it but it didn't. I haven't used guard statements before. – Hugo Zhang Nov 12 '17 at 01:48
  • 1
    Check out my answer, I think this should work out your problem. The reason the guard let statement wasn't working was because you were declaring a variable which needs a value. To read more on guard let statements check out [this](https://stackoverflow.com/questions/32256834/swift-guard-vs-if-let). You generally want to always use them when you have optional variables. – Thomas Wang Nov 12 '17 at 02:13

1 Answers1

2

I think I got it. Try this:

guard let uid = Auth.auth().currentUser?.uid else { return }

    Database.database().reference().child("images_" + uid + "_links/CoverPhotos").observeSingleEvent(of: .value, with: { (snapshot) in
        for rest in snapshot.children.allObjects as! [DataSnapshot] {
        //do stuff
        }
    })
Thomas Wang
  • 2,233
  • 2
  • 14
  • 24