【发布时间】:2015-11-14 03:32:26
【问题描述】:
我正在尝试生成一个名为“areaList”的 PFObject 数组。我已经对此进行了大量研究,并且了解到我可以从使用完成处理程序来处理加载结果的异步性质中受益。具体来说,我的要求是获得一些关于我做错了什么的指导,以及关于如何“更好”地实现结果的潜在提示。
这是我的带有完成处理程序的查询函数:
func loadAreasNew(completion: (result: Bool) -> ()) -> [Area] {
var areaList = self.areaList
let areaQuery = PFQuery(className: "Area")
areaQuery.findObjectsInBackgroundWithBlock {
(areas: [PFObject]?, error: NSError?) -> Void in
if error == nil {
for area in areas! {
let areaToAdd = area as! Area
areaList.append(areaToAdd)
// print(areaList) // this prints the list each time
// print(areaToAdd) // this prints the converted Area in the iteration
// print(area) // this prints the PFObject in the iteration
if areaList.count == areas!.count {
completion(result: true)
} else {
completion(result: false)
}
}
} else {
print("There was an error")
}
}
return areaList
}
这是我尝试在 viewDidLoad 中调用它的方式:
loadAreasNew { (result) -> () in
if (result == true) {
print(self.areaList)
} else {
print("Didn't Work")
}
}
我在 viewDidLoad 之前分配了这个变量:
var areaList = [Area]()
在控制台中,我得到以下信息:
Didn't Work
Didn't Work
Didn't Work
Didn't Work
[]
代表我知道 Parse 中的 5 个项目...
【问题讨论】:
标签: ios swift parse-platform