Swift Async完成以返回自定义类中的Array

Alp*_*ven 5 arrays class block swift completion

我将尽力解释这个问题.

我正在使用Parse.com并从Parse数据库类返回数据.我想把这个parse.com调用放在自定义类中的自己的函数中.我遇到的问题是完成.它去哪儿了?我已经尝试了很多不同版本的添加到我的功能,但它无法正常工作.

以下是获取类名,表名和排序描述符并返回数组的函数:

func queryDataInBackgroundWithBlock(parseClass:String, parseObject:String, sortDescriptor:NSSortDescriptor) -> [Any]
Run Code Online (Sandbox Code Playgroud)

当我添加完成时,我使用(可能不正确):

func queryDataInBackgroundWithBlock(parseClass:String, parseObject:String, sortDescriptor:NSSortDescriptor, completion: (result: Any)->Void)
Run Code Online (Sandbox Code Playgroud)

现在在函数内部,我使用Parse.com代码来获取数据

    query.findObjectsInBackgroundWithBlock {
        (objects: [AnyObject]!, error: NSError!) -> Void in
        if error == nil {

            // Do something with the found objects
            for object in objects {
                self.arrayOfObjects.append(object[parseObject]!)
            }

        } else {
            // Log details of the failure
            println("Error: \(error) \(error.userInfo!)")
        }

    }
Run Code Online (Sandbox Code Playgroud)

我的目标是将参数发送到我的类函数,从parse.com获取数据,然后在异步调用之后将数据作为数组返回

我想这样称呼它:

myClass.queryDataInBackgroundWithBlock("databaseName", parseObject: "columnName", sortDescriptor: orderBy){
    (result: Any) in
    println(result)
}
Run Code Online (Sandbox Code Playgroud)

这几乎就像是嵌套完成.完成后如何返回数组?是交给函数然后返回它,还是需要在嵌套代码中返回,或者是什么?它检索数据,但问题是完成后返回.

更新:正如我在下面的评论中所述:

query.findObjectsInBackgroundWithBlock({
    (objects: [AnyObject]!, error: NSError!) -> Void in
    if error == nil {

        // Do something with the found objects
        for object in objects {
            self.arrayOfObjects.append(object[parseObject]!)
        }

    } else {
        // Log details of the failure
        println("Error: \(error) \(error.userInfo!)")
    }

}, completion: {
     //do something here
})
Run Code Online (Sandbox Code Playgroud)

这是返回错误:"调用额外参数完成"我不知道如何在块的末尾添加完成,所以我在块周围添加了()并插入完成.这显然是错误的,但我不知道如何在亚马逊建议的块结束时添加完成

更新2:

    func queryDataInBackgroundWithBlock(parseClass:String, parseObject:String, sortDescriptor:NSSortDescriptor) -> [Any]{
        var query = PFQuery(className:parseClass)

        if sortDescriptor.key != "" {
            query.orderBySortDescriptor(sortDescriptor)
        }

        query.findObjectsInBackgroundWithBlock {
            (objects: [AnyObject]!, error: NSError!) -> Void in
            if error == nil {

                // Do something with the found objects
                for object in objects {
                    self.arrayOfObjects.append(object[parseObject]!!)
                }

            } else {
                // Log details of the failure
                println("Error: \(error) \(error.userInfo!)")
            }

        }

        return self.arrayOfObjects  //<-- needs to move to completion
    }
Run Code Online (Sandbox Code Playgroud)

mat*_*att 6

在函数内部,queryDataInBackgroundWithBlock您将收到名称下的完成块completion.它需要一个参数.所以,在你获得数据之后,你做的最后一件事就是调用它,将数据交给它:

completion(result:myData)
Run Code Online (Sandbox Code Playgroud)

因为query.findObjectsInBackgroundWithBlock它本身是异步的,所以你需要将该调用作为块的最后一个内容query.findObjectsInBackgroundWithBlock.

像这样:

func queryDataInBackgroundWithBlock(
    parseClass:String, parseObject:String, sortDescriptor:NSSortDescriptor, 
    completion: (result: Any)->Void) 
{
    var query = PFQuery(className:parseClass)

    if sortDescriptor.key != "" {
        query.orderBySortDescriptor(sortDescriptor)
    }

    query.findObjectsInBackgroundWithBlock {
        (objects: [AnyObject]!, error: NSError!) -> Void in
        if error == nil {

            // Do something with the found objects
            for object in objects {
                self.arrayOfObjects.append(object[parseObject]!!)
            }

        } else {
            // Log details of the failure
            println("Error: \(error) \(error.userInfo!)")
        }
        completion(result:self.arrayOfObjects)
    }

}
Run Code Online (Sandbox Code Playgroud)