React Native Native Module 在 iOS 上循环调用导致崩溃
各位小伙伴们,大家好呀!看看今天我又给各位带来了什么文章?本文标题是《React Native Native Module 在 iOS 上循环调用导致崩溃》,很明显是关于Golang的文章哈哈哈,其中内容主要会涉及到等等,如果能帮到你,觉得很不错的话,欢迎各位多多点评和分享!
我正在尝试在 ios 上的 react native 中构建 grpc 客户端。
对于上下文:react native 不直接支持 grpc,必须从 swift 调用自定义本机模块,该模块进行 grpc 调用并返回值。
grpc 服务器是本地编译的 golang 模块,它使用 http2server 模块。我没有编写 grpc 服务器,因此我无法更改它的代码。
react native 方法似乎正在循环执行本机 grpc 客户端调用,导致 golang 的 http2server 模块崩溃。
此 grpc 客户端调用是从按钮 onpress() 事件调用的,而不是从循环调用的。我尝试将 grpc 调用包装在超时测试中,以防止调用太快。
我的本机模块有一个导出函数,如下所示:
@objc(swiftnativegrpcclient) class swiftnativegrpcclient: nsobject {
// ...
@objc func swiftgetgrpctest(
_ resolve: rctpromiseresolveblock,
rejecter reject: rctpromiserejectblock
) {
print("swiftnativegrpcclient.swiftgetgrpctest()")
// connect to grpc channel if necessary
if (self.securegrpcchannel == nil) {
self.createsecurechannel()
}
// out of paranoia, don't let this call be used less than
// once per second
if (getmillisecondssincelastcall() < 1000) {
print("method called to soon.")
reject("0", "method called too soon", nil)
return
}
let grpcserviceclient = service_servicename(channel: self.securegrpcchannel!)
let testrequest: service_testrequest = service_testrequest()
// service_testresponse should contain a string:
// "grpc response success"
let testresponse: service_testresponse
let testcall = grpcserviceclient.gettest(testrequest)
do {
try testresponse = testcall.response.wait()
print(testresponse)
} catch {
print("rpc method 'getinfo' failed \(error)")
return
}
// update the last call time to ensure this isn't being called
// more than once per second
self.lastcalltime = dispatchtime.now()
resolve(getinforesponse)
}
// ...
}
我的 react native 调用本机模块是这样的:
const { swiftnativegrpcclient } = nativemodules;
export default function app() {
const nativegrpcclient = useref(swiftnativegrpcclient)
const [lastcalltime, setlastcalltime] = usestate(new date())
const rngetgrpctest = async () => {
try {
const currenttime = new date()
console.log(`lastcalltime: ${lastcalltime}`)
console.log(`currenttime: ${currenttime}`)
const timediff = currenttime - lastcalltime
console.log(`timediff: ${timediff} ms`)
// just checking... don't let this method
// be executed more than once per second
if (timediff > 1000) {
await nativegrpcclient.current.swiftgetgrpctest()
}
} catch (error) {
console.error(error.message)
}
setlastcalltime(currenttime)
}
// ...
}
xcode 输出如下所示
- 看起来 grpc 客户端正在对 grpc 服务器进行多次调用。在崩溃之前,您会看到 react native 模块发射器发出大约 20 次相同的响应
2021-12-01 15:23:56.400068+0200 testapp[13091:123303] [javascript] { output: 'swiftnativegrpcclient.swiftgetgrpctest()\n' }
2021-12-01 15:23:58.698908+0200 testapp[13091:123303] [javascript] got output from naive module emitter:
2021-12-01 15:23:58.699576+0200 testapp[13091:123303] [javascript] { output: '1638365038 [inf] test.go:3294 grpc response success\n' }
2021-12-01 15:23:58.700075+0200 testapp[13091:123303] [javascript] got output from naive module emitter:
2021-12-01 15:23:58.700606+0200 testapp[13091:123303] [javascript] { output: '1638365038 [inf] test.go:3294 grpc response success\n' }
2021-12-01 15:23:58.701067+0200 testapp[13091:123303] [javascript] got output from naive module emitter:
2021-12-01 15:23:58.701596+0200 testapp[13091:123303] [javascript] { output: '1638365038 [inf] test.go:3294 grpc response success\n' }
2021-12-01 15:23:58.702036+0200 testapp[13091:123303] [javascript] got output from naive module emitter:
2021-12-01 15:23:58.702726+0200 testapp[13091:123303] [javascript] { output: '1638365038 [inf] test.go:3294 grpc response success\n' }
2021-12-01 15:23:58.704172+0200 testapp[13091:123303] [javascript] got output from naive module emitter:
2021-12-01 15:23:58.704766+0200 testapp[13091:123303] [javascript] { output: '1638365038 [inf] test.go:3294 grpc response success\n' }
2021-12-01 15:23:58.705121+0200 testapp[13091:123303] [javascript] got output from naive module emitter:
2021-12-01 15:23:58.705497+0200 testapp[13091:123303] [javascript] { output: '1638365038 [inf] test.go:3294 grpc response success\n' }
2021-12-01 15:23:58.705833+0200 testapp[13091:123303] [javascript] got output from naive module emitter:
2021-12-01 15:23:58.715472+0200 testapp[13091:123303] [javascript] { output: '1638365038 [inf] test.go:3294 grpc response success\n' }
panic: 2021-12-01 15:23:58.715856+0200 testapp[13091:123303] [javascript] got output from naive module emitter:
2021-12-01 15:23:58.716342+0200 testapp[13091:123303] [javascript] { output: '1638365038 [inf] test.go:3294 grpc response success\n' }
2021-12-01 15:23:58.716751+0200 testapp[13091:123303] [javascript] got output from naive module emitter:
2021-12-01 15:23:58.717020+0200 testapp[13091:123303] [javascript] { output: '1638365038 [inf] test.go:3294 grpc response success\n' }
2021-12-01 15:23:58.717247+0200 testapp[13091:123303] [javascript] got output from naive module emitter:
2021-12-01 15:23:58.717510+0200 testapp[13091:123303] [javascript] { output: '1638365038 [inf] test.go:3294 grpc response success\n' }
2021-12-01 15:23:58.718216+0200 testapp[13091:123303] [javascript] got output from naive module emitter:
close of closed channel
goroutine 24507 [2021-12-01 15:23:58.718544+0200 testapp[13091:123303] [javascript] { output: '1638365038 [inf] test.go:3294 grpc response success\n' }
running]:
2021-12-01 15:23:58.718827+0200 testapp[13091:123303] [javascript] got output from naive module emitter:
2021-12-01 15:23:58.719167+0200 testapp[13091:123303] [javascript] { output: '1638365038 [inf] test.go:3294 grpc response success\n' }
- 通过本机 swift 模块向 react native 返回响应后,golang 的 http2server 在
handleping()方法期间崩溃。看起来 grpc 连接已关闭,然后再次尝试关闭,http2server 没有妥善处理该连接
这是 xcode 控制台日志:
2021-12-01 15:23:58.717247+0200 testApp[13091:123303] [javascript] Got output from Naive Module Emitter:
2021-12-01 15:23:58.717510+0200 testApp[13091:123303] [javascript] { output: '1638365038 [INF] test.go:3294 gRPC response success\n' }
2021-12-01 15:23:58.718216+0200 testApp[13091:123303] [javascript] Got output from Naive Module Emitter:
close of closed channel
goroutine 24507 [2021-12-01 15:23:58.718544+0200 testApp[13091:123303] [javascript] { output: '1638365038 [INF] test.go:3294 gRPC response success\n' }
running]:
goroutine 24507 [2021-12-01 15:23:58.718544+0200 testApp[13091:123303] [javascript] { output: '1638365038 [INF] test.go:3294 gRPC response success\n' }
running]:
2021-12-01 15:23:58.718827+0200 testApp[13091:123303] [javascript] Got output from Naive Module Emitter:
2021-12-01 15:23:58.719167+0200 testApp[13091:123303] [javascript] { output: '1638365038 [INF] test.go:3294 gRPC response success\n' }
google.golang.org/grpc/internal/transport.(*http2Server).handlePing(0xc00007a1c0, 0xc003c08090)
google.golang.org/[email protected]/internal/transport/http2_server.go:680 +0x6d
google.golang.org/grpc/internal/transport.(*http2Server).HandleStreams(0xc00015d800, 0xc0029d0f68, 0x10a742005)
google.golang.org/[email protected]/internal/transport/http2_server.go:494 +0x31f
google.golang.org/grpc.(*Server).serveStreams(0xc000499860, {0x10b916390, 0xc00015d800})
google.golang.org/[email protected]/server.go:742 +0x114
google.golang.org/grpc.(*Server).handleRawConn.func1()
google.golang.org/[email protected]/server.go:703 +0x34
created by google.golang.org/grpc.(*Server).handleRawConn
google.golang.org/[email protected]/server.go:702 +0x405
CoreSimulator 757.5 - Device: iPhone SE (2nd generation) (ECBD797A-E2B4-49F2-9DD5-BC8FB95EFACC) - Runtime: iOS 14.5 (18E182) - DeviceType: iPhone SE (2nd generation)
当我使用完全相同的 swift 代码创建一个测试项目,但没有 react native 前端时,我没有遇到此崩溃。 react native 以某种方式参与了崩溃行为,可能是由于 native 模块的功能如何发挥作用?
有人知道如何防止这种循环发生吗?
正确答案
如果其他人也遇到类似问题,
我的问题与循环的出现无关。
我需要在 swift 函数结束时关闭 grpc 通道。这可以使用 defer 语句来完成:
@objc(SwiftNativeGrpcClient) class SwiftNativeGrpcClient: NSObject {
// ...
@objc func swiftGetGrpcTest(
_ resolve: RCTPromiseResolveBlock,
rejecter reject: RCTPromiseRejectBlock
) {
if (self.secureGrpcChannel == nil) {
self.createSecureChannel()
}
let grpcServiceClient = Service_ServiceName(channel: self.secureGrpcChannel!)
defer {
// close the channel when the method exits
try? grpcServiceClient.channel.close().wait()
}
let testRequest: Service_TestRequest = Service_TestRequest()
// Service_TestResponse should contain a String:
// "gRPC response success"
let testResponse: Service_TestResponse
let testCall = grpcServiceClient.getTest(testRequest)
do {
try testResponse = testCall.response.wait()
print(testResponse)
resolve(getInfoResponse)
} catch {
print("RPC method failed \(error)")
reject("0","RPC method failed: \(error)", nil)
return
}
// update the last call time to ensure this isn't being called
// more than once per second
}
// ...
}
循环控制台输出是由两件事的相互作用引起的:
- 我正在捕获标准输出并通过
rcteventemitter路由它 - 事件之间未清除缓冲区。
因此,rcteventemitter 似乎多次收到 grpc 成功消息。
终于介绍完啦!小伙伴们,这篇关于《React Native Native Module 在 iOS 上循环调用导致崩溃》的介绍应该让你收获多多了吧!欢迎大家收藏或分享给更多需要学习的朋友吧~golang学习网公众号也会发布Golang相关知识,快来关注吧!
规避联网激活:如何新电脑设置
- 上一篇
- 规避联网激活:如何新电脑设置
- 下一篇
- 可以对两个列进行分组吗?
-
- Golang · Go问答 | 1年前 |
- 在读取缓冲通道中的内容之前退出
- 139浏览 收藏
-
- Golang · Go问答 | 1年前 |
- 戈兰岛的全球 GOPRIVATE 设置
- 204浏览 收藏
-
- Golang · Go问答 | 1年前 |
- 如何将结构作为参数传递给 xml-rpc
- 325浏览 收藏
-
- Golang · Go问答 | 1年前 |
- 如何用golang获得小数点以下两位长度?
- 478浏览 收藏
-
- Golang · Go问答 | 1年前 |
- 如何通过 client-go 和 golang 检索 Kubernetes 指标
- 486浏览 收藏
-
- Golang · Go问答 | 1年前 |
- 将多个“参数”映射到单个可变参数的习惯用法
- 439浏览 收藏
-
- Golang · Go问答 | 1年前 |
- 将 HTTP 响应正文写入文件后出现 EOF 错误
- 357浏览 收藏
-
- Golang · Go问答 | 1年前 |
- 结构中映射的匿名列表的“复合文字中缺少类型”
- 352浏览 收藏
-
- Golang · Go问答 | 1年前 |
- NATS Jetstream 的性能
- 101浏览 收藏
-
- Golang · Go问答 | 1年前 |
- 如何将复杂的字符串输入转换为mapstring?
- 440浏览 收藏
-
- Golang · Go问答 | 1年前 |
- 相当于GoLang中Java将Object作为方法参数传递
- 212浏览 收藏
-
- Golang · Go问答 | 1年前 |
- 如何确保所有 goroutine 在没有 time.Sleep 的情况下终止?
- 143浏览 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 543次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 516次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 500次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 485次学习
-
- ChatExcel酷表
- ChatExcel酷表是由北京大学团队打造的Excel聊天机器人,用自然语言操控表格,简化数据处理,告别繁琐操作,提升工作效率!适用于学生、上班族及政府人员。
- 3186次使用
-
- Any绘本
- 探索Any绘本(anypicturebook.com/zh),一款开源免费的AI绘本创作工具,基于Google Gemini与Flux AI模型,让您轻松创作个性化绘本。适用于家庭、教育、创作等多种场景,零门槛,高自由度,技术透明,本地可控。
- 3398次使用
-
- 可赞AI
- 可赞AI,AI驱动的办公可视化智能工具,助您轻松实现文本与可视化元素高效转化。无论是智能文档生成、多格式文本解析,还是一键生成专业图表、脑图、知识卡片,可赞AI都能让信息处理更清晰高效。覆盖数据汇报、会议纪要、内容营销等全场景,大幅提升办公效率,降低专业门槛,是您提升工作效率的得力助手。
- 3429次使用
-
- 星月写作
- 星月写作是国内首款聚焦中文网络小说创作的AI辅助工具,解决网文作者从构思到变现的全流程痛点。AI扫榜、专属模板、全链路适配,助力新人快速上手,资深作者效率倍增。
- 4535次使用
-
- MagicLight
- MagicLight.ai是全球首款叙事驱动型AI动画视频创作平台,专注于解决从故事想法到完整动画的全流程痛点。它通过自研AI模型,保障角色、风格、场景高度一致性,让零动画经验者也能高效产出专业级叙事内容。广泛适用于独立创作者、动画工作室、教育机构及企业营销,助您轻松实现创意落地与商业化。
- 3807次使用
-
- GoLand调式动态执行代码
- 2023-01-13 502浏览
-
- 用Nginx反向代理部署go写的网站。
- 2023-01-17 502浏览
-
- Golang取得代码运行时间的问题
- 2023-02-24 501浏览
-
- 请问 go 代码如何实现在代码改动后不需要Ctrl+c,然后重新 go run *.go 文件?
- 2023-01-08 501浏览
-
- 如何从同一个 io.Reader 读取多次
- 2023-04-11 501浏览

