有没有一种方法可以异步返回我的意图响应?当我处理好自己的意图时,我正在发出网络请求,我想等待完成快捷方式流程,直到成功发出此请求。
我目前有:
func handle(intent: AppleScriptIntent, completion: @escaping (AppleScriptIntentResponse) -> Void) {
let response = AppleScriptIntentResponse(code: .success, userActivity: nil)
let data = intent.apple_script?.data(using: .utf8)
let networkHandler = NetworkHandler()
networkHandler.browseForService()
let connection = networkHandler.connection
connection.send(content: data, completion: .idempotent)
completion(response)
}
但是我想要拥有:
func handle(intent: AppleScriptIntent, completion: @escaping (AppleScriptIntentResponse) -> Void) {
let successResponse = AppleScriptIntentResponse(code: .success, userActivity: nil)
let inProgressResponse = AppleScriptIntentResponse(code: .inProgress, userActivity: nil)
let data = intent.apple_script?.data(using: .utf8)
let networkHandler = NetworkHandler()
networkHandler.browseForService()
let connection = networkHandler.connection
connection.send(content: data, completion: .idempotent)
if requestSent {
completion(successResponse)
} else {
completion(inProgressResponse)
}
}
即使网络连接和请求需要5秒钟才能完成,该最后一个代码段也会运行并立即完成快捷方式。