Learn Courses My Dashboard

Trying to check if a value already exists in fire base

I have a problem where I am trying to check if firebase already has a value, then based off of it existing or not, do a different operation. But I don’t know how to check if a piece of data already exists. Curious if there is a simple way of doing this or if it will be above my head.

Hi @Ru55_2

Welcome to the community.

The way to do this would be to create a query on the document that you are looking for and if the querySnapshot!.documents.isEmpty == true then you know is does not exist.

For example:

    func Lesson11Example1() {
        let db = Firestore.firestore()
        let reservations = db.collection("reservations")
        let query = reservations.whereField("name", in: ["Carol", "Dave"])

        query.getDocuments { (querySnapshot, error) in
            if let error = error {
                print(error.localizedDescription)
            } else {
                if querySnapshot!.documents.isEmpty {
                    print("No data Found")
                } else {
                    // Loop through documents and print data.
                    for doc in querySnapshot!.documents {
                        print(doc.data())
                    }
                }
            }
        }

If the query does not find “Dave” or “Carol” in any of the documents under the collection “reservations” then the query wont fail but will just return nothing in the querySnapshot.

If it returns the data you are looking for then set something accordingly and do what ever you need to do.

Does that make sense?

@Chris_Parker Yes this makes sense, but I’m using the real-time data base, would that be any different or is it just worth using the new one that they have?

I haven’t used the realtime database to any degree so can’t offer any advice.

1 Like

The code is relatively the same, but check Firebase docs to make sure you’re up to date!

1 Like

@Chris_Parker this search method doesn’t seem to be working, not sure why but no matter if the value is there or not it will return as if it isn’t there

@Ru55_2

Is that the code I sent you that you are referring to?

@Chris_Parker
yes i’m using the unzipped files you sent me

But did you try using the whole project as it is rather than pick out the files in it?

@Chris_Parker

yeah i downloaded it then did the pod update

Did you change to your Team in the Signing and Capabilities?

1 Like

yeah i did everything for it to run and work

If you set a breakpoint in your ViewController file on, say, the line that is:

query.getDocuments { (querySnapshot, error) in

and then run the code, does it get to that point?

I’m not really sure what you mean, like add a line space after that?

No.
To set a breakpoint, click on the line number where that line of code is. You should see this appear:

Yours might be blue rather than the color I have.

This is how you debug your code.

Now run the program in your simulator.

yeah it ran when i did that

Yes.

You should have a symbol in the Console that says lldb: which is the debug prompt where you can type in commands.

yup that showed up when i tried to trigger that part of the code