gpt4 book ai didi

ios - 将 findObjectsInBackgroundWithBlock 的结果传递到变量中

转载 作者:行者123 更新时间:2023-11-29 02:03:04 25 4
gpt4 key购买 nike

我正在尝试将查询结果传递到变量数组

var petitions = [PFObject] = []

然后返回结果。我如何在 Swift 中做到这一点?

func getPetitions(employeeId: String, employeeBusiness: String) -> [PFObject] {

var petitions: [PFObject] = []

var query = PFQuery(className:"Petitions")
query.selectKeys(["petitionDate", "availableFrom", "availableTo"])
query.whereKey("employeeId", equalTo:employeeId)
query.whereKey("employeeBusiness", equalTo:employeeBusiness)
query.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]?, error: NSError?) -> Void in
if error == nil {
if let objects = objects as? [PFObject] {
NSLog("Successfully retrieved \(objects.count) petitions.")
for object in objects {
petitions.append(object)
}
}
}
}

return petitions
}

最佳答案

查询是异步传递的,这意味着当您返回请求时,query.findObjectsInBackgroundWithBlock可能不会(99%不会)完成。

你可以直接在函数中做你想做的事情:

var petitions: [PFObject] = [] //class variable

func getPetitions(employeeId: String, employeeBusiness: String) {

var query = PFQuery(className:"Petitions")
query.selectKeys(["petitionDate", "availableFrom", "availableTo"])
query.whereKey("employeeId", equalTo:employeeId)
query.whereKey("employeeBusiness", equalTo:employeeBusiness)
query.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]?, error: NSError?) -> Void in
if error == nil {
if let objects = objects as? [PFObject] {
NSLog("Successfully retrieved \(objects.count) petitions.")
for object in objects {
self.petitions.append(object)
}
// update UI
// or just do whatever you want with the petitions.
}
}
}
}

或者你可以编写一个 block 函数:

func getPetitions(employeeId: String, employeeBusiness: String, block:PFArrayResultBlock) {

var query = PFQuery(className:"Petitions")
query.selectKeys(["petitionDate", "availableFrom", "availableTo"])
query.whereKey("employeeId", equalTo:employeeId)
query.whereKey("employeeBusiness", equalTo:employeeBusiness)
query.findObjectsInBackgroundWithBlock(block)
}
}

调用它:

getPetitions("employeeId", "employeeBusiness", block: {
(objects: [AnyObject]?, error: NSError?) -> Void in
// do whatever you want with your objects..
// update UI
// or just do whatever you want with the petitions.
})

解决这个问题的另一种方法是同步查询:

func getPetitions(employeeId: String, employeeBusiness: String) -> [PFObject] {
var query = PFQuery(className:"Petitions")
query.selectKeys(["petitionDate", "availableFrom", "availableTo"])
query.whereKey("employeeId", equalTo:employeeId)
query.whereKey("employeeBusiness", equalTo:employeeBusiness)
var objects = query.findObjects()
if let petititons = objects as? [PFObject] {
return petititons
} else {
return [PFObject]() // return an empty pf object array. If you want, return nil.. but the return value must be [PFObject]? if you do this.
}
}

关于ios - 将 findObjectsInBackgroundWithBlock 的结果传递到变量中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30114389/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com