Swift 3/iOS UIView not updating after retrieving remote JSON data -


i have uitableview list of users. when tap on row, uid of user passed uiviewcontroller detail view. urlrequest made retrieve json data of user (username, avatar, etc). however, detail view inconsistently updates information. it'll show users' name, avatar, etc other times it'll show nothing or it'll show username or show avatar, etc.

in fetchuser() method, have print("username: \(self.user.username)") shows correct data being retrieved 100% of time won't display 100% of time in view.

any appreciated.

thanks!

class profileviewcontroller: uiviewcontroller {      @iboutlet weak var avatarimageview: uiimageview!     @iboutlet weak var usernamelabel: uilabel!     @iboutlet weak var networthlabel: uilabel!      var user: user!     var uid: int?      override func viewwillappear(_ animated: bool) {         super.viewwillappear(animated)     }      override func viewdidload() {         super.viewdidload()          // additional setup after loading view.         fetchuser()     }      func reloadview() {         self.usernamelabel.text = user.username         self.networthlabel.text = "$" + numberformatter.localizedstring(from: int((user.networth)!)! nsnumber, number: numberformatter.style.decimal)         self.avatarimageview.downloadimage(from: user.avatar!)         circularimage(photoimageview: self.avatarimageview)     }      func fetchuser() {         // post user data server         let myurl = nsurl(string: "http://localhost/test/profile")         let urlrequest = nsmutableurlrequest(url: myurl! url);         urlrequest.httpmethod = "post"          let poststring = "uid=\(uid!)"          urlrequest.httpbody = poststring.data(using: string.encoding.utf8)          let task = urlsession.shared.datatask(with: urlrequest urlrequest) { (data, response, error) in              if (error != nil) {                 print("error=\(string(describing: error))")                 return             } // end if              self.user = user()              {                  let json = try jsonserialization.jsonobject(with: data!, options: .mutablecontainers) as? [string : anyobject]                  if let parsejson = json?["data"] as? [[string : anyobject]] {                      userfromjson in parsejson {                          let userdata = user()                          if let uid = userfromjson["uid"] as? string,                             let username = userfromjson["username"] as? string,                             let networth = userfromjson["networth"] as? string,                             let avatar = userfromjson["avatar"] as? string {                              userdata.uid = int(uid)                             userdata.username = username                             userdata.networth = networth                             userdata.avatar = avatar                              self.usernamelabel.text = username                             self.networthlabel.text = networth                             self.avatarimageview.downloadimage(from: avatar)                             circularimage(photoimageview: self.avatarimageview)                          } // end if                          self.user = userdata                      } // end                  } // end if                  dispatchqueue.main.async {                     print("username: \(self.user.username)")                     self.reloadview()                 }              } catch let error {                 print(error)             }         }         task.resume()      } 

firstly, call fetch user in viewwillappear this:

override func viewwillappear(_ animated: bool) { super.viewwillappear(animated) fetchuser() }

then, change code here did, don't use reloadview function had, instead, update ui elements on main thread @ end of fetchuser function. changed weren't updating ui twice because have 4 lines @ bottom of if let uid = ... statement in fetchuser updated ui elements wasn't in main thread why in version removed 4 lines of code. let me know if worked you.

let task = urlsession.shared.datatask(with: urlrequest urlrequest) { (data, response, error) in          if (error != nil) {             print("error=\(string(describing: error))")             return         } // end if          self.user = user()          {              let json = try jsonserialization.jsonobject(with: data!, options: .mutablecontainers) as? [string : anyobject]              if let parsejson = json?["data"] as? [[string : anyobject]] {                  userfromjson in parsejson {                      let userdata = user()                      if let uid = userfromjson["uid"] as? string,                         let username = userfromjson["username"] as? string,                         let networth = userfromjson["networth"] as? string,                         let avatar = userfromjson["avatar"] as? string {                          userdata.uid = int(uid)                         userdata.username = username                         userdata.networth = networth                         userdata.avatar = avatar                      } // end if                      self.user = userdata                  } // end              } // end if              dispatchqueue.main.async {                 self.usernamelabel.text = user.username                 self.networthlabel.text = "$" + numberformatter.localizedstring(from: int((user.networth)!)! nsnumber, number: numberformatter.style.decimal)                 self.avatarimageview.downloadimage(from: user.avatar!)                 circularimage(photoimageview: self.avatarimageview)             }          } catch let error {             print(error)         }     }     task.resume() 

Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -