1

我在这个请求上遇到的问题是第一个函数syncRequest总是返回 nil ,因为函数在 (reply, error) 返回填写我的返回字典之前退出。

有没有办法让它在退出我的关闭之前等待回调返回?

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

非常感谢您对我们的帮助!

4

2 回答 2

4

您是否syncRequest()可以在准备好后使用结果调用闭包?将定义更改为:

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

然后在通话结束时createRequest(),您可以调用回调 on 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)
        }
    }()
}
于 2015-01-27T04:50:02.510 回答
-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))
}
于 2016-03-11T15:35:39.503 回答