0
votes

Im trying to access to my firebase db with Swift

import Foundation
import FirebaseDatabase
import FirebaseAuth
import Firebase

struct FirebaseManager {

    private let reference = Database.database().reference()

    func fetch (_ child: String, completion: @escaping (DataSnapshot) -> Void) {
        print("before reference")
        reference
            .child(child)
            .observeSingleEvent(of: .value) { (snapdata) in
            print("inside reference")
            guard let response = snapdata.value else { return print("error snapdata") }
            print(response)
            completion(snapdata)
        }
        print("after reference")
    }
}

To check if code is running correctly, I put some prints and this is what I see in Xcode console:

before reference
after reference

There's neither "inside reference" nor response value nor "error snapdata"

So I've deduced it never goes inside it ! And I checked: my firebase db is not empty and corresponds to child parameter string.

I don't understand where is the problem.

Same problem here : iOS Swift - Firebase query never gets called

EDIT: I also tried this:

.observeSingleEvent(of: .value, with: { (snapshot) in
    print("observe single event")
            if snapshot.exists() {
                if let value = snapshot.value as? String {
                    print("inside if let", value)
                }
            } else {
                print("else")
                // username doesn't exist
            }

        }, withCancel:{ error in
         print("cancel", error)
        })

And the ouput in console is the same. Nothing appears

And this is a screenshot in rules:

enter image description here

1
You might want to use observeSingleEvent(of:with:withCancel:) instead, to see if the query gets cancelled by the database. If it does, the user does not have permission to read the data, and you should read the documentation on security rules.Frank van Puffelen

1 Answers

0
votes

You might want to use observeSingleEvent(of:with:withCancel:) instead, to see if the query gets cancelled by the database. If it does, the user does not have permission to read the data, and you should read the documentation on security rules.