首页 > 解决方案 > 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。

标签: asynchronousobjective-c-blocksgoogle-speech-api

解决方案


我认为这不是 gRPC 或这个 API 应该如何工作的。对于 gRPC,在收到响应(Operation 对象)后,它的值将不再变化,因此对其进行 while-looping 肯定会导致无限循环。LongRunningRecognize 方法是一个异步 RPC 方法,所以 operation.done == false 在这种情况下只是意味着这个操作仍在服务器端运行。您将需要操作服务中的方法来进一步与服务器交互以获取此请求的结果。


推荐阅读