Swift如何在退出函数之前等待回调完成?

时间:2015-01-27 04:05:54

标签: swift callback wait apple-watch

我对此请求的问题是第一个函数 syncRequest 总是返回nil,因为函数退出之前(回复,错误)回来填写我的返回字典。

有没有办法让它在返回闭包之前等待回调?

public typealias KKWatchSyncResponse = Dictionary<String, AnyObject>

func syncRequest() -> KKWatchSyncResponse? {
    var syncResponseDict : KKWatchSyncResponse?
    createRequest(KKWatchRequest.Sync, parameter: nil) { reply, error in
        if reply == nil || error != nil {
            return
        } else {
            syncResponseDict = KKWatchSyncResponse()
        }
        if let songInfo = NSKeyedUnarchiver.unarchiveObjectWithData(reply!["songInfo"] as NSData) as NSDictionary? {
            syncResponseDict!["songInfo"] = songInfo
        }
        if let albumArtImage = NSKeyedUnarchiver.unarchiveObjectWithData(reply!["albumArt"] as NSData) as? UIImage {
            syncResponseDict!["albumArtImage"] = albumArtImage
        }
        if let isPlaying = NSKeyedUnarchiver.unarchiveObjectWithData(reply!["isPlaying"] as NSData) as? Bool {
            syncResponseDict!["isPlaying"] = isPlaying
        }
    }()
    return syncResponseDict
}

    func createRequest(request:KKWatchRequest, parameter: KKWatchAPIRequestParameter?, callback:KKWatchAPICallback) -> KKWatchAPIParentRequest {
       var requestDict : Dictionary<String, AnyObject> = [KKBOXWatchAppRequestType : request.rawValue]
        if parameter != nil {
        requestDict += parameter! //Combine 2 dictionaries
    }
        return { WKInterfaceController.openParentApplication(requestDict){ reply, error in
                callback(reply, error)
        }
    }
}

非常感谢您的帮助!

2 个答案:

答案 0 :(得分:4)

你可以让syncRequest()接受一个在结束时调用结果的闭包吗?将定义更改为:

func syncRequest(callback:(KKWatchSyncResponse?)->Void) { ... }

然后在createRequest()电话结束时,您可以在syncResponseDict上拨打回叫,因为现在已经填充了您的数据...... callback(syncResponseDict)

编辑:这是我想到的解决方案。

func syncRequest(callback:(KKWatchSyncResponse?)->Void) {
    createRequest(KKWatchRequest.Sync, parameter: nil) { reply, error in            
        if reply == nil || error != nil {
            callback(nil)
        } else {
            var syncResponseDict : KKWatchSyncResponse? = KKWatchSyncResponse()
            if let songInfo = NSKeyedUnarchiver.unarchiveObjectWithData(reply!["songInfo"] as NSData) as NSDictionary? {
                syncResponseDict!["songInfo"] = songInfo
            }
            if let albumArtImage = NSKeyedUnarchiver.unarchiveObjectWithData(reply!["albumArt"] as NSData) as? UIImage {
                syncResponseDict!["albumArtImage"] = albumArtImage
            }
            if let isPlaying = NSKeyedUnarchiver.unarchiveObjectWithData(reply!["isPlaying"] as NSData) as? Bool {
                syncResponseDict!["isPlaying"] = isPlaying
            }
            callback(syncResponseDict)
        }
    }()
}

答案 1 :(得分:-1)

直接实现锁定变量。这对进行异步网络加载的单元测试最有帮助。

func waitingFunction()
{
     //set a lock during your async function
     var locked = true
     RunSome.asyncFunction() { () -> Void in

       //after your sync function remove the lock
       locked = false
     })

     //wait for the async method to complete before advancing
     while(locked){wait()}

     //move on from the lock
     doMoreStuff()
}
func wait()
{
    NSRunLoop.currentRunLoop().runMode(NSDefaultRunLoopMode, beforeDate: NSDate(timeIntervalSinceNow: 1))
}