我有一个奇怪的问题。以下代码块放置在第一个 View Controller 的 viewDidAppear 部分中,当它运行时, println(latArray)、println(longArray)、println(addressArray) 都没有返回值。在控制台中,它为所有三个数组返回 []。但是,当我转到另一个 View Controller 并返回时,它将填充来自 Parse 的数据。为什么是这样?为什么首次使用 viewWillAppear 方法加载应用程序时不会填充 latArray、longArray 和 addressArray?
var query = PFQuery(className: "NewLog")
// Add a where clause if there is a search criteria
query.whereKey("Type", containsString: newRecordCreated)
println(query)
query.findObjectsInBackgroundWithBlock({
(objects, error) -> Void in
if error == nil {
// Results were successfully found
if let objects = objects as? [PFObject] {
for object in objects {
//println(object["follower"])
latArray.append(object["Lat"] as! Double)
longArray.append(object["Long"] as! Double)
addressArray.append(object["Address"] as! String)
}
}
// self.tableView.reloadData()
} else {
println("error")
// The network was inaccessible and we have no cached data for
// this query.
}
})
println(latArray)
println("^^Latitude values")
println(longArray)
println("^^Longitude values")
println(addressArray)
println("^^Address values")
}
最佳答案
查询正在后台执行。查询完成时执行 block (闭包)中的代码。注意:这可能并且最有可能是在 viewDidAppear
完成之后。如果将 print 语句放在 for 循环之后,您应该会看到值。如果取消注释 reloadData
方法,则在查询完成时应使用新信息更新表。
关于ios - Parse 中的查询类(swift),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31487093/