将findObjectsInBackgroundWithBlock的结果传递给变量

时间:2015-05-08 01:34:15

标签: ios swift parse-platform

我试图将查询结果传递给变量数组

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
}

2 个答案:

答案 0 :(得分:1)

该查询是异步传递的,这意味着当您返回query.findObjectsInBackgroundWithBlock时,petitions可能不会(99%赢得&#t; t)完成。

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

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.
            }
        }
    }
}

或者你可以写一个块功能:

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.
    }
}

答案 1 :(得分:0)

你误解了完成块。您无法像这样从异步调用返回结果。相反,你需要使函数成为void(无结果),然后将处理结果的代码放在完成块中。

您应该重构getPetitions方法,将其自己的完成块作为参数。完成异步Parse调用并且您要完成附加项目的代码后,您将调用提供的块。