mercredi 19 juillet 2017

Firebase IOS Swift : How To pick Random userProfile?

I am new at Firebase, and i am trying to build an app where its shows an random user profile when users start the app ("Kinda like Tinder")

after a couple hours testing stuff, i found an solution it works but if feels wrong! i call an observer inside another observer, is this good? two connections??? i don't know but it feels bad! but i can't find another solution.

Can someone help me? is my code good or bad? if isn't correct can someone give me some good advise how to do it?

my firebase Database structure:

"profiles": {
    "1": {
      "name": "John Doe",
      "age": "36",
      "location": "Los Angeles",
      "gender": "male",
    },
    "2": { ... },
    "3": { ... }
  },

my Swift IOS Firebase Code

var profilesRef: DatabaseReference!
let profilesRef = Database.database().reference().child("profiles")

profilesRef.observe(.value, with: { (snapshot) in

    let profilesCount = snapshot.childrenCount
    let diceRoll : String = String(arc4random_uniform(UInt32(postsCount)))

    profilesRef.child(diceRoll).observeSingleEvent(of: .value, with: { (snapshot) in
        if let profileDict = snapshot.value {
            let profile =  Profile(profileData: profileDict as! Dictionary<String, AnyObject>)
            print(profile.age)
        }
    })
})

it works but it feels wrong. :) I hope you guys can help me, thanks in advance




Aucun commentaire:

Enregistrer un commentaire