问题描述
我正在尝试将 GRPC 与 React Native 结合使用。虽然基本的 helloWorld 示例可以很好地使用它。当我向helloworld.proto 文件添加新请求时,它不会生成/更新Helloworld.prbc.h 文件。
helloworld.proto
Syntax = "proto3";
option objc_class_prefix = "HLW";
package helloworld;
// The greeting service deFinition.
service Greeter {
// Sends a greeting
rpc SayHello (HelloRequest) returns (HelloReply) {}
rpc HelloDev (HelloDevRequest) returns (HelloDevReply) {}
}
// The request message containing the user's name.
message HelloRequest {
string name = 1;
}
// The response message containing the greetings
message HelloReply {
string message = 1;
}
// The request message containing the user's name.
message HelloDevRequest {
string name = 1;
}
// The response message containing the greetings
message HelloDevReply {
string message = 1;
}
我试过了:
pod install
在 pod install 之后,我期待 Helloworld.prbc.h 有 HelloDev 的定义。 另一种方式:
protoc --objc_out=Pods/ReactNativeGrpc --objcgrpc_out=Pods/ReactNativeGrpc helloworld.proto
ReactNativeGrpc 是我项目的 pod
这里是编译器自动生成的文件(即使在运行 pod install 之后也是一样的)
// Code generated by gRPC proto compiler. DO NOT EDIT!
// source: helloworld.proto
#import <Foundation/Foundation.h>
#if !defined(GPB_GRPC_FORWARD_DECLARE_MESSAGE_PROTO) || !GPB_GRPC_FORWARD_DECLARE_MESSAGE_PROTO
#import "Helloworld.pbobjc.h"
#endif
#if !defined(GPB_GRPC_PROTOCOL_ONLY) || !GPB_GRPC_PROTOCOL_ONLY
#import <ProtoRPC/ProtoService.h>
#import <ProtoRPC/ProtoRPCLegacy.h>
#import <RxLibrary/GRXWriteable.h>
#import <RxLibrary/GRXWriter.h>
#endif
@class HLWHelloReply;
@class HLWHelloRequest;
#if !defined(GPB_GRPC_FORWARD_DECLARE_MESSAGE_PROTO) || !GPB_GRPC_FORWARD_DECLARE_MESSAGE_PROTO
#endif
@class GRPCUnaryProtoCall;
@class GRPCStreamingProtoCall;
@class GRPCCallOptions;
@protocol GRPCProtoResponseHandler;
@class GRPCProtoCall;
NS_ASSUME_NONNULL_BEGIN
@protocol HLWGreeter2 <NSObject>
#pragma mark SayHello(HelloRequest) returns (HelloReply)
/**
* Sends a greeting
*/
- (GRPCUnaryProtoCall *)sayHelloWithMessage:(HLWHelloRequest *)message responseHandler:(id<GRPCProtoResponseHandler>)handler callOptions:(GRPCCallOptions *_Nullable)callOptions;
@end
/**
* The methods in this protocol belong to a set of old APIs that have been deprecated. They do not
* recognize call options provided in the initializer. Using the v2 protocol is recommended.
*/
@protocol HLWGreeter <NSObject>
#pragma mark SayHello(HelloRequest) returns (HelloReply)
/**
* Sends a greeting
*
* This method belongs to a set of APIs that have been deprecated. Using the v2 API is recommended.
*/
- (void)sayHelloWithRequest:(HLWHelloRequest *)request handler:(void(^)(HLWHelloReply *_Nullable response,NSError *_Nullable error))handler;
/**
* Sends a greeting
*
* This method belongs to a set of APIs that have been deprecated. Using the v2 API is recommended.
*/
- (GRPCProtoCall *)RPCToSayHelloWithRequest:(HLWHelloRequest *)request handler:(void(^)(HLWHelloReply *_Nullable response,NSError *_Nullable error))handler;
@end
#if !defined(GPB_GRPC_PROTOCOL_ONLY) || !GPB_GRPC_PROTOCOL_ONLY
/**
* Basic service implementation,over gRPC,that only does
* marshalling and parsing.
*/
@interface HLWGreeter : GRPCProtoService<HLWGreeter2,HLWGreeter>
- (instancetype)initWithHost:(Nsstring *)host callOptions:(GRPCCallOptions *_Nullable)callOptions NS_DESIGNATED_INITIALIZER;
+ (instancetype)serviceWithHost:(Nsstring *)host callOptions:(GRPCCallOptions *_Nullable)callOptions;
// The following methods belong to a set of old APIs that have been deprecated.
- (instancetype)initWithHost:(Nsstring *)host;
+ (instancetype)serviceWithHost:(Nsstring *)host;
@end
#endif
NS_ASSUME_NONNULL_END
PodFile:
platform :ios,'9.0'
source 'https://github.com/CocoaPods/Specs.git'
require_relative '../node_modules/@react-native-community/cli-platform-ios/native_modules'
def add_flipper_pods!(versions = {})
versions['Flipper'] ||= '~> 0.33.1'
versions['DoubleConversion'] ||= '1.1.7'
versions['Flipper-Folly'] ||= '~> 2.1'
versions['Flipper-Glog'] ||= '0.3.6'
versions['Flipper-PeerTalk'] ||= '~> 0.0.4'
versions['Flipper-RSocket'] ||= '~> 1.0'
pod 'FlipperKit',versions['Flipper'],:configuration => 'Debug'
pod 'FlipperKit/FlipperKitLayoutPlugin',:configuration => 'Debug'
pod 'FlipperKit/SKIOSNetworkPlugin',:configuration => 'Debug'
pod 'FlipperKit/FlipperKitUserDefaultsPlugin',:configuration => 'Debug'
pod 'FlipperKit/FlipperKitReactPlugin',:configuration => 'Debug'
# List all transitive dependencies for FlipperKit pods
# to avoid them being linked in Release builds
pod 'Flipper',:configuration => 'Debug'
pod 'Flipper-DoubleConversion',versions['DoubleConversion'],:configuration => 'Debug'
pod 'Flipper-Folly',versions['Flipper-Folly'],:configuration => 'Debug'
pod 'Flipper-Glog',versions['Flipper-Glog'],:configuration => 'Debug'
pod 'Flipper-PeerTalk',versions['Flipper-PeerTalk'],:configuration => 'Debug'
pod 'Flipper-RSocket',versions['Flipper-RSocket'],:configuration => 'Debug'
pod 'FlipperKit/Core',:configuration => 'Debug'
pod 'FlipperKit/CppBridge',:configuration => 'Debug'
pod 'FlipperKit/FBCxxFollyDynamicConvert',:configuration => 'Debug'
pod 'FlipperKit/FBDefines',:configuration => 'Debug'
pod 'FlipperKit/FKPortForwarding',:configuration => 'Debug'
pod 'FlipperKit/FlipperKitHighlightOverlay',:configuration => 'Debug'
pod 'FlipperKit/FlipperKitLayoutTextSearchable',:configuration => 'Debug'
pod 'FlipperKit/FlipperKitNetworkPlugin',:configuration => 'Debug'
end
# Post Install processing for Flipper
def flipper_post_install(installer)
installer.pods_project.targets.each do |target|
if target.name == 'YogaKit'
target.build_configurations.each do |config|
config.build_settings['SWIFT_VERSION'] = '4.1'
end
end
end
end
target 'Hello' do
# Pods for Hello
pod 'ReactNativeGrpc',:path => '.'
pod 'FblazyVector',:path => "../node_modules/react-native/Libraries/FblazyVector"
pod 'FBReactNativeSpec',:path => "../node_modules/react-native/Libraries/FBReactNativeSpec"
pod 'RCTrequired',:path => "../node_modules/react-native/Libraries/RCTrequired"
pod 'RCTTypeSafety',:path => "../node_modules/react-native/Libraries/TypeSafety"
pod 'React',:path => '../node_modules/react-native/'
pod 'React-Core',:path => '../node_modules/react-native/'
pod 'React-CoreModules',:path => '../node_modules/react-native/React/CoreModules'
pod 'React-Core/DevSupport',:path => '../node_modules/react-native/'
pod 'React-RCTActionSheet',:path => '../node_modules/react-native/Libraries/ActionSheetIOS'
pod 'React-RCTAnimation',:path => '../node_modules/react-native/Libraries/NativeAnimation'
pod 'React-RCTBlob',:path => '../node_modules/react-native/Libraries/Blob'
pod 'React-RCtimage',:path => '../node_modules/react-native/Libraries/Image'
pod 'React-RCTLinking',:path => '../node_modules/react-native/Libraries/LinkingIOS'
pod 'React-RCTNetwork',:path => '../node_modules/react-native/Libraries/Network'
pod 'React-RCTSettings',:path => '../node_modules/react-native/Libraries/Settings'
pod 'React-RCTText',:path => '../node_modules/react-native/Libraries/Text'
pod 'React-RCTVibration',:path => '../node_modules/react-native/Libraries/Vibration'
pod 'React-Core/RCTWebSocket',:path => '../node_modules/react-native/'
pod 'React-cxxreact',:path => '../node_modules/react-native/ReactCommon/cxxreact'
pod 'React-jsi',:path => '../node_modules/react-native/ReactCommon/jsi'
pod 'React-jsiexecutor',:path => '../node_modules/react-native/ReactCommon/jsiexecutor'
pod 'React-jsinspector',:path => '../node_modules/react-native/ReactCommon/jsinspector'
pod 'ReactCommon/callinvoker',:path => "../node_modules/react-native/ReactCommon"
pod 'ReactCommon/turbomodule/core',:path => "../node_modules/react-native/ReactCommon"
pod 'Yoga',:path => '../node_modules/react-native/ReactCommon/yoga',:modular_headers => true
pod 'DoubleConversion',:podspec => '../node_modules/react-native/third-party-podspecs/DoubleConversion.podspec'
pod 'glog',:podspec => '../node_modules/react-native/third-party-podspecs/glog.podspec'
pod 'Folly',:podspec => '../node_modules/react-native/third-party-podspecs/Folly.podspec'
target 'HelloTests' do
inherit! :complete
# Pods for testing
end
use_native_modules!
# Enables Flipper.
#
# Note that if you have use_frameworks! enabled,Flipper will not work and
# you should disable these next few lines.
add_flipper_pods!
post_install do |installer|
flipper_post_install(installer)
end
end
target 'Hello-tvOS' do
# Pods for Hello-tvOS
target 'Hello-tvOSTests' do
inherit! :search_paths
# Pods for testing
end
end
ReactNativeGRPC.Podspec:
Pod::Spec.new do |s|
s.name = "ReactNativeGrpc"
s.version = "0.0.1"
s.license = "Apache License,Version 2.0"
s.authors = { 'gRPC contributors' => 'grpc-io@googlegroups.com' }
s.homepage = "https://grpc.io/"
s.summary = "Example integration between react-native and GRPC"
s.source = { :git => 'https://github.com/grpc/grpc.git' }
s.ios.deployment_target = "7.1"
s.osx.deployment_target = "10.9"
# Base directory where the .proto files are.
src = "../proto"
# Run protoc with the Objective-C and gRPC plugins to generate protocol messages and gRPC clients.
s.dependency "!ProtoCompiler-gRPCPlugin","~> 1.0"
# Pods directory corresponding to this app's Podfile,relative to the location of this podspec.
pods_root = 'Pods'
# Path where Cocoapods downloads protoc and the gRPC plugin.
protoc_dir = "#{pods_root}/!ProtoCompiler"
protoc = "#{protoc_dir}/protoc"
plugin = "#{pods_root}/!ProtoCompiler-gRPCPlugin/grpc_objective_c_plugin"
# Directory where the generated files will be placed.
dir = "#{pods_root}/#{s.name}"
s.prepare_command = <<-CMD
mkdir -p #{dir}
#{protoc} \
--plugin=protoc-gen-grpc=#{plugin} \
--objc_out=#{dir} \
--grpc_out=#{dir} \
-I #{src} \
-I #{protoc_dir} \
#{src}/helloworld.proto
CMD
# Files generated by protoc
s.subspec "Messages" do |ms|
ms.source_files = "#{dir}/*.pbobjc.{h,m}","#{dir}/**/*.pbobjc.{h,m}"
ms.header_mappings_dir = dir
ms.requires_arc = false
# The generated files depend on the protobuf runtime.
ms.dependency "Protobuf"
end
# Files generated by the gRPC plugin
s.subspec "Services" do |ss|
ss.source_files = "#{dir}/*.pbrpc.{h,"#{dir}/**/*.pbrpc.{h,m}"
ss.header_mappings_dir = dir
ss.requires_arc = true
# The generated files depend on the gRPC runtime,and on the files generated by protoc.
ss.dependency "gRPC-ProtoRPC"
ss.dependency "#{s.name}/Messages"
end
s.pod_target_xcconfig = {
# This is needed by all pods that depend on Protobuf:
'GCC_PREPROCESSOR_DEFinitioNS' => '$(inherited) GPB_USE_PROTOBUF_FRAMEWORK_IMPORTS=1',# This is needed by all pods that depend on gRPC-RxLibrary:
'CLANG_ALLOW_NON_MODULAR_INCLUDES_IN_FRAMEWORK_MODULES' => 'YES',}
end
编辑 1:每次清洁和安装都有效,但需要替代方法来避免它。
解决方法
您是否尝试过清理 Cocoapods 缓存?如果没有,请在运行 pod install
之前尝试运行以下命令来清理缓存:
rm -rf Pods
rm -rf (your_project).xcworkspace
rm Podfile.lock