asynchronous - Google 长时间运行语音 API 的 Objective-C 异步调用未返回操作状态为真?
问题描述
我在使用 Google 异步语音识别长期运行 API 时遇到问题。operation.done
没有返回真 。
我修改了 Objective-C 示例程序https://github.com/GoogleCloudPlatform/ios-docs-samples/blob/master/speech/Objective-C/Speech-gRPC-Nonstreaming/Speech/SpeechRecognitionService.m以使用 longruning API .
这是修改后的片段 -
// prepare a single gRPC call to make the request
GRPCProtoCall *call = [client RPCToLongRunningRecognizeWithRequest:recognizeRequest
handler:
^(Operation *operation, NSError *nserror) {
if (nserror) {
NSLog(@"ERROR: %@", nserror);
completion([nserror description]);
} else {
NSLog(@"RESPONSE name %@", operation.name);
while (!operation.done) {
NSLog(@"operation done - %d", operation.done);
usleep(2000000);
}
GPBAny *gpbAny = operation.response;
NSLog(@"RESPONSE typeURL %@", gpbAny.typeURL);
NSLog(@"RESPONSE deescription %@", gpbAny.value.description);
GPBMessage *longRunningResponse = [gpbAny unpackMessageClass:LongRunningRecognizeResponse.class error:nil];
NSLog(@"RESPONSE RECEIVED %@", longRunningResponse);
completion(longRunningResponse);
}
}];
它永远不会从while (!operation.done)
循环中出来。operation.name
返回正确的操作 ID。我能够使用gcloud ml speech operations describe 2104003022050949209
命令验证调用转到谷歌语音 API 并返回转录的消息。但是 Objective-C 代码不返回 operation.done true。
解决方案
我认为这不是 gRPC 或这个 API 应该如何工作的。对于 gRPC,在收到响应(Operation 对象)后,它的值将不再变化,因此对其进行 while-looping 肯定会导致无限循环。LongRunningRecognize 方法是一个异步 RPC 方法,所以 operation.done == false 在这种情况下只是意味着这个操作仍在服务器端运行。您将需要操作服务中的方法来进一步与服务器交互以获取此请求的结果。
推荐阅读
- php - PHP 意外的文件结尾,在 jwt 登录页面中
- mysql - Kafka Mysql Connector plugin.path 配置
- python - 如何根据命令运行定时器?
- jquery - 为传单 L.Draggable 对象设置包含
- jquery - 如何在 each() 函数中设置密钥?
- python - 绘制边缘宽度与其权重成正比的图形
- angular - 当我使用 ng serve 运行时,如何在我的 Angular6 项目中使用 ionic4 组件
- shell - 查找所有子文件夹并复制其内容
- python - 像 C++ 中的 cin >> 这样从一行中一个接一个地读取元素
- sql-server - 如何阻止来自所有 Microsoft SQL Server 进程的传出网络请求?