ios - Loading data from network for a TableViewController -


in app, have viewcontroller obtains data user, segue tableviewcontroller. tableviewcontroller displays data acquired rest call remote server.

i have coded http request in viewdidload, , when data received, call

self.tableview.reloaddata() 

but takes forever (5-10 sec.) data appear in tableview, after rest call completes.

should place rest call elsewhere? instance, better data retrieval in initial viewcontroller, segue when data ready?

i don't think code relevant, here code inside viewdidload:

override func viewdidload() {     super.viewdidload()      self.tableview.contentinset = uiedgeinsetsmake(20.0, 0.0, 0.0, 0.0)      // uncomment following line preserve selection between presentations     // self.clearsselectiononviewwillappear = false      // display edit button in navigation bar view controller.     self.navigationitem.rightbarbuttonitem = self.editbuttonitem()      datamanager.spsearch { (teamsjson) -> void in         let json = json(data: teamsjson)         //println(json)          if let teamarray = json["d"]["query"]["primaryqueryresult"]["relevantresults"]["table"]["rows"]["results"].array {             teamdict in teamarray {                 //println(teamdict)                 if let teamcells = teamdict["cells"]["results"].array {                     var teamtitle: string = ""                     var teamurl: string = ""                     teamcell in teamcells {                         // each cell contains value of specific managed metadata                         // validate if on right 1                         if teamcell["key"].string == "title" {                             println("title=" + teamcell["value"].string!)                             teamtitle = teamcell["value"].string!                         }                         if teamcell["key"].string == "spweburl" {                             println("spweburl=" + teamcell["value"].string!)                             teamurl = teamcell["value"].string!                             let tenantlength = count(globaltenant)                             // remove string containing tenant frmo beginning                             teamurl = suffix(teamurl, count(teamurl) - count(globaltenant))                         }                     }                      var team = team(name: teamtitle, url: teamurl)                     self.teams.append(team)                 }             }             println("nb of teams= \(self.teams.count)")             self.tableview.reloaddata()         }     }  } 

it seems call reloaddata on background thread. try this:

nsoperationqueue.mainqueue().addoperationwithblock {     self.tableview.reloaddata() } 

Comments