在我的应用程序中,我有一个ViewController从用户获取数据,然后segue到TableViewController。我的TableViewController显示从REST调用获取的数据到远程服务器。
我已经在ViewDidLoad中编写了http请求,当收到数据时,我会调用
self.tableView.reloadData()
但是,在REST调用完成后,数据显示在TableView中需要花费很长时间(5-10秒)。
我应该将我的REST呼叫放在其他地方吗?例如,最好在初始ViewController中进行所有数据检索,然后在所有数据准备就绪时进行segue?
我不认为我的代码是相关的,但这里是我的ViewDidLoad中的代码:
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.contentInset = UIEdgeInsetsMake(20.0, 0.0, 0.0, 0.0)
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Display an Edit button in the navigation bar for this 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 {
for teamDict in teamArray {
//println(teamDict)
if let teamCells = teamDict["Cells"]["results"].array {
var teamTitle: String = ""
var teamUrl: String = ""
for teamCell in teamCells {
// Each cell contains the value of a specific managed metadata
// validate if we are on the right one
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 the string containing the tenant frmo the 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()
}
}
}
答案 0 :(得分:1)
您似乎在后台线程上调用了reloadData
。试试这个:
NSOperationQueue.mainQueue().addOperationWithBlock {
self.tableView.reloadData()
}