Dominik
Dominik

Reputation: 419

Swift Firebase Query started by every change in Database

i created an App which loads Data from Firebase. To save ressources and make the app economical as possible i implemented queries to load suitable data for the client. Thats the code:

    ref.queryOrdered(byChild: "name").queryEqual(toValue: "test").observe(.value, with:{ (snapshot: DataSnapshot) in

        if snapshot.exists(){
            ref.queryOrdered(byChild: "hometown").queryEqual(toValue: "munich").observe(.value, with:{ (snapshot: DataSnapshot) in


                for users in snapshot.children.allObjects as! [DataSnapshot]{
                    if users.key as! String != userId{
                        let item = users.value as? [String: AnyObject]
                        let userid = users.key as! String
                        let Username  = item?["name"] as! String
                        let Userage = item?["age"] as! String
                        let ProfileImage = item?["profileImageUrl"] as! String
                        let firstImage = item?["firstimage"] as! String
                        let seccondImage = item?["seccondimage"] as! String
                        let thirdImage = item?["thirimage"] as! String
                        let fourthImage = item?["fourthimage"] as! String
                        let fifthImage = item?["fifthimage"] as! String
                        let sixtImage = item?["sixtimage"] as! String
                        let sex = item?["sex"] as! String
                        let gender = item?["gender"] as! String
                        let info = item?["info"] as! String
                        let userItem = UserObject(userId: userid, name: Username, age: Userage, sex: sex, gender: gender, profileImage: ProfileImage, fistImage: firstImage, seccondImage: seccondImage, thirdImage: thirdImage, fourthImage: fourthImage, fifthImage: fifthImage, info: info, sixtImage: sixtImage)
                          self.checkValidFences(userItem: userItem, gf: gf)

                    }
                }


            })
        }

    })

unfortunately the method loads always data when something changed in the Database. Thats the reason why i always get redundant values after changing something in the database. Is there a way to parse value just once

Upvotes: 0

Views: 103

Answers (2)

Peter Haddad
Peter Haddad

Reputation: 80914

You can solve this by Reading data once

You can use the observeSingleEventOfType method to simplify this scenario: the event callback added triggers once and then does not trigger again.


observeSingleEvent(of:with:)

This is equivalent to observeEventType:withBlock:, except the block is immediately canceled after the initial data is returned.

more info here:

https://firebase.google.com/docs/reference/swift/firebasedatabase/api/reference/Classes/DatabaseReference#observesingleeventofwith

Upvotes: 2

Shehata Gamal
Shehata Gamal

Reputation: 100503

You may need observeSingleEvent instead of observe

so replace every

.observe(.value

with

.observeSingleEvent(.value

See here in Docs

Upvotes: 0

Related Questions