我正在为基于CocoaAsyncSocket的UDP通讯编写一个Nativescript Plugin,但是当我试图在这个pod的上下文中执行任何async操作时,我的应用程序就会崩溃.这似乎与iOS x {N}上的队列线程有关,但我一直无法解决它,有什么想法吗?
下面是我如何在Typescript中创建我的Plugin iOS类。
export class UdpProtocol extends NSObject implements GCDAsyncUdpSocketDelegate
以下是我如何初始化我的 GCDAsyncUdpSocket
:
let dispatchQueue = dispatch_queue_create("sendUnicast_queue", null);
let sock = GCDAsyncUdpSocket.alloc()
.initWithDelegateDelegateQueue(this, dispatchQueue);
下面是我在.ts中调用async方法的一个例子。
sock.sendDataToHostPortWithTimeoutTag(sendData, address, port, -1, this.tag);
这是我期望正确启动的回调。
udpSocketDidSendDataWithTag(sock: GCDAsyncUdpSocket, tag: number): void {
console.log('udpSocketDidSendDataWithTag callback');
}
但不幸的是,我得到的是下面的JS异常。
CONSOLE LOG file:///node_modules/@nativescript/core/inspector_modules.js:1:0: Loading inspector modules...
CONSOLE LOG file:///node_modules/@nativescript/core/inspector_modules.js:6:0: Finished loading inspector modules.
NativeScript debugger attached.
***** Fatal JavaScript exception - application has been terminated. *****
Native stack trace:
1 0x11033960e NativeScript::reportFatalErrorBeforeShutdown(JSC::ExecState*, JSC::Exception*, bool)
2 0x110388f76 -[TNSRuntimeInspector reportFatalError:]
3 0x10fc2d23b TNSInspectorUncaughtExceptionHandler
4 0x7fff23e3a36d __handleUncaughtException
5 0x7fff50ad7c05 _objc_terminate()
6 0x7fff4f926c87 std::__terminate(void (*)())
7 0x7fff4f926c29 std::terminate()
8 0x7fff519128df _dispatch_client_callout
9 0x7fff5191860c _dispatch_lane_serial_drain
10 0x7fff51919044 _dispatch_lane_invoke
11 0x7fff519230c4 _dispatch_workloop_worker_thread
12 0x7fff51b37a3d _pthread_wqthread
13 0x7fff51b36b77 start_wqthread
JavaScript stack trace:
UIApplicationMain(file:///node_modules/@nativescript/core/application/application.js:312:0)
at run(file:///node_modules/@nativescript/core/application/application.js:312:0)
at file:///node_modules/@nativescript/angular/platform-common.js:210:0
at file:///node_modules/@nativescript/angular/platform-common.js:111:0
at file:///node_modules/@nativescript/angular/platform-common.js:91:0
at file:///app/bundle.js:198:144
at ./main.ts(file:///app/bundle.js:203:34)
at __webpack_require__(file:///src/webpack/bootstrap:750:0)
at checkDeferredModules(file:///src/webpack/bootstrap:43:0)
at webpackJsonpCallback(file:///src/webpack/bootstrap:30:0)
at anonymous(file:///app/bundle.js:2:61)
at evaluate([native code])
at moduleEvaluation([native code])
at [native code]
at asyncFunctionResume([native code])
at [native code]
at promiseReactionJob([native code])
JavaScript error:
file:///node_modules/@nativescript/core/application/application.js:312:0: JS ERROR Error: -[__NSCFString bytes]: unrecognized selector sent to instance 0x6000036496c0
NativeScript caught signal 6.
Native Stack:
1 0x11038a251 sig_handler(int)
2 0x7fff51b2f5fd _sigtramp
3 0x7fff51a23f39 itoa64
4 0x7fff51a1fb7c abort
5 0x7fff4f927858 abort_message
6 0x7fff4f926cad std::__terminate(void (*)())
7 0x7fff4f926c29 std::terminate()
8 0x7fff519128df _dispatch_client_callout
9 0x7fff5191860c _dispatch_lane_serial_drain
10 0x7fff51919044 _dispatch_lane_invoke
11 0x7fff519230c4 _dispatch_workloop_worker_thread
12 0x7fff51b37a3d _pthread_wqthread
13 0x7fff51b36b77 start_wqthread
JS Stack unavailable. Current thread hasn't initialized a {N} runtime.
我已经尝试过这样的解决方案 此处但没有成功。
导致该问题的Angular演示插件可用于 此肱骨
非常欢迎任何提示或帮助 :)
事实上,我已经发现,该错误是与我如何创建 发送数据 变量,我把它从。
let sendData: any = NSString.alloc().initWithCString(msg);
改成了
const sendData: NSData = NSData.alloc().initWithBase64EncodedStringOptions(msg, 1);