在Swift中传递和存储闭包/回调

时间:2015-04-02 20:05:22

标签: swift callback closures

我想在swift代码中执行以下操作:

我必须打电话给我的api才能更新几个项目。所以我异步调用每个项目的api。每个api调用在完成后执行回调函数。这些回调减少了一个计数器,所以当计数器达到0时,我知道所有的api调用都已完成。当计数器达到0时,我想调用最终回调函数(一次,当所有调用完成时),以便更新我的UI等等。最后的回调是在开始时传递给我的服务并存储在类属性中以便以后执行。

可执行游乐场来源:

// Playground - noun: a place where people can play

class MyService
{
    let api = MyApi()

    var storedFinalCallback: () -> Void = { arg in }
    var queue: Int                      = 0

    func update(items: [String], finalCallback: () -> Void )
    {
        // Count the necessary API calls
        queue               = items.count
        // Store callback for later execution
        storedFinalCallback = finalCallback

        for item in items {
            // Call api per item and pass queueCounter as async callback
            api.updateCall(item, callback: self.callback())
        }
    }

    func callback()
    {
        queue--
        // Execute final callback when queue is empty
        if queue == 0 {
            println("Executing final callback")
            storedFinalCallback()
        }
    }

}

class MyApi
{
    func updateCall(item: String, callback: ())
    {
        println("Updating \(item)")
    }
}

let myItems: [String]     = ["Item1", "Item2", "Item3"]
let myInstance: MyService = MyService()

myInstance.update(myItems, finalCallback: {() -> Void in
    println("Done")
})

问题是,使用此代码,最后的回调将以错误的顺序调用。 As the console output from the playground shows.

显然回调函数已经执行但没有正确传递。但是,这是我能够做到的唯一方法,没有编译器错误。

任何帮助都会非常感激 - 我已经坚持了两天了。

1 个答案:

答案 0 :(得分:6)

我终于找到了工作代码:

// Playground - noun: a place where people can play

class MyService
{
    let api = MyApi()

    var storedFinalCallback: () -> Void = { arg in }
    var queue: Int                      = 0

    func update(items: [String], finalCallback: () -> Void )
    {
        // Count the necessary API calls
        queue               = items.count
        // Store callback for later execution
        storedFinalCallback = finalCallback

        for item in items {
            // Call api per item and pass queueCounter as async callback
            api.updateCall(item, callback: self.callback)
        }
    }

    func callback()
    {
        queue--
        // Execute final callback when queue is empty
        if queue == 0 {
            println("Executing final callback")
            storedFinalCallback()
        }
    }

}

class MyApi
{
    func updateCall(item: String, callback: () -> Void)
    {
        println("Updating \(item)")
        callback()
    }
}

let myItems: [String]     = ["Item1", "Item2", "Item3"]
let myInstance: MyService = MyService()

myInstance.update(myItems, finalCallback: {() -> Void in
    println("Done")
})