我正在编写一个使用解析服务器(由 heroku 托管)数据库的应用程序。我有几个从数据库中提取信息的函数,但它们本质上都是异步的(因为 parse 的.findObjectinBackground
工作方式。)这个问题是因为后面的数据库查询需要来自先前查询的信息。由于被拉取的信息是异步的,我决定实现 PromiseKit 以确保findObjectinBackground
在运行第二个查询之前从第一个查询中找到对象。查询的一般形式如下:
let query = PFQuery(classname: "Hello")
query?.findObjectsInBackground(block: { (objects, error) in
if let objectss = objects{
for object in objectss{ //object needs to be pulled
arrayOfInterest.append(object)
//array must be appended before moving on to next query
}
}
})
我只是不知道该怎么做。这是我想实现它的方式:
import PromiseKit
override func viewDidLoad(){
when(/*query object is retrieved/array is appended*/).then{
//perform the next query
}
}
我根本不知道在when()
和中放入什么.then{}
。我尝试将查询放入它们自己的单独函数中,并在这两个(何时和然后)函数中调用它们,但我基本上被告知我不能,因为它们返回 void。此外,我不能简单地确保第一个查询在 中运行,when()
因为query.findObjectinBackground
(在查询中)是异步的。在下一个可以触发之前,特别需要拉出对象,而不仅仅是查询运行。