ios - Why does an instance variable have a different value inside of a closure? -


func loadyelpcomments(){         guard let business = business else {return}         guard let _ = tableview else {return}         guard let businessid = business.id else {return}         let dateformatter = dateformatter()         dateformatter.datestyle = .full          httphelper.getyelpcomments(for: businessid, completionhandler: { data in             let json = json(data)             let reviews = json["reviews"].arrayvalue.map({return $0.dictionaryvalue})             var commentdate: date?               (index, review) in reviews.enumerated(){                 let userdictionary = review["user"]?.dictionary                 if let datestring = review["time_created"]?.stringvalue{                     commentdate = dateformatter.date(from: datestring)                 }                  let yelpcomment = yelpcomment(rating: review["rating"]?.intvalue, userimageurl: userdictionary?["image_url"]?.stringvalue, username: userdictionary?["name"]?.stringvalue, comment: review["text"]?.stringvalue, commentdate: commentdate, commentpageurl: review["url"]?.stringvalue)                  self.comments.append(yelpcomment)             }              print("number of comments: \(self.comments.count)") //prints: number of comments: 3"             self.tableview.reloaddata()          })              print("number of comments: \(self.comments.count)") //this prints firt "number of comments: 0"      } 

the getyelpcomments(for:completionhandler:) class method responsible fetching json data yelp api. surprise though comments instance array being updated appending new yelpcomment objects it, count has different values inside , outside of closure.

these comments supposed displayed in table view. further confuses me fact when add tableview.reloaddata() within closure index out of bounds empty array error when add same line of code: tableview.reloaddata() out side of closure no error , comments.count equates zero. appreciated!

p.s. don't know if mentioning going make difference data @escaping. using swiftyjson , alamofire.

update:

my table view data source methods are:

   override func tableview(_ tableview: uitableview, numberofrowsinsection section: int) -> int {          if section == 0 {             return super.tableview(tableview, numberofrowsinsection: section)         } else {             return comments.count         }      }      override func tableview(_ tableview: uitableview, cellforrowat indexpath: indexpath) -> uitableviewcell {          if indexpath.section == 0 {             return super.tableview(tableview, cellforrowat: indexpath)         } else {             let cell = tableview.dequeuereusablecell(withidentifier: customcelltypeidentifiers.yelpcommentcell, for: indexpath) as! yelpcommentcell             cell.configureyelpcell(with: comments[indexpath.row])             return cell         }     } 

because job of completion block report when network call done. httphelper making call server, can take considerable amount of time. not stop program executing, , wait @ line: goes next line, immediately, , calls print("number of comments: \(self.comments.count)"), , 0, because network call isn't done yet. then, time later, whole completion block happens.

this called asynchronous function, , commonly trips people haven't seen before, see lots of eventually, it's worth reading on.

the fact "when add tableview.reloaddata() within closure index out of bounds empty array error", sounds 1 of uitableviewdatasource functions configures table (cellforrowat, numberofrowsinsection), has error in somewhere.


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -