ios - Setting user values from Firebase -


i trying initialize values in user object question seems it's not possible there - now, trying same thing in view controller in viewdidload, i'm running error:

this call firebase in viewdidload (myuser global variable var myuser:user!):

        let userref: firdatabasereference = firdatabase.database().reference().child("users").childbyautoid()         let userrefhandle: firdatabasehandle?          userrefhandle = userref.observe(.value, with: { (snapshot) -> void in             let userdata = snapshot.value as! dictionary<string, anyobject>             let id = snapshot.key              if (firauth.auth()?.currentuser?.uid) != nil {                 if let name = userdata["name"] as! string!, name.characters.count > 0 {                     let handle = userdata["handle"] as! string!                     let gender = userdata["gender"] as! string!                     let profilepicture = userdata["profilepicture"] as! string!                     let uid = userdata["uid"] as! string!                     // let rooms = userdata["rooms"] as! [[string : anyobject]]                      myuser.uid = uid                     myuser.uid = handle                     myuser.uid = name                     myuser.uid = profilepicture                     myuser.uid = gender //                        myuser.rooms = rooms                  } else {                     print("error! not initialize user data firebase")                 }             }         }) 

the end goal when user launches app (already signed , info in firebase), info pulled database , set user object values can used around app (name, handle, profilepicture, etc.).

i'm getting error: could not cast value of type 'nsnull' (0x106d378c8) 'nsdictionary' on let userdata = snapshot.value line.

this user's data looks in firebase:

"users" : {     "-kgjw9pevcpvqzn7t5pz" : {       "gender" : "male",       "handle" : "testhandle123",       "name" : "timothy",       "profilepicture" : "https://graph.facebook.com/*removed*/picture?type=large&return_ssl_resources=1",       "uid" : "2q4vcku1e7hil84obdzgqcq0ph63"     } } 

i'm wondering if correct way set user's values firebase, , if so, how avoid error?

in code, looking profile picture using profile

let profilepicture = userdata["profile"] as! string! 

but in data, have profilepicture

because have forced unwrap error when key not found.

you should make sure use same key in both places. might worth including defaults in case ever have data issues

let profilepicture = userdata["profile"] as? string ?? "default" 

Comments

Popular posts from this blog

inversion of control - Autofac named registration constructor injection -

verilog - Systemverilog dynamic casting issues -

ios - Change Storyboard View using Seague -