objective c - React native bridge is sometimes nil in swift module -


i have swift module created, starts listening on gcdasyncudpsocket when connect method called swift

@objc(mymodule) class mymodule: nsobject, gcdasyncudpsocketdelegate {   var bridge: rctbridge!   var socket: gcdasyncudpsocket!    func methodqueue() -> dispatch_queue_t {     return dispatch_queue_create("com.mycompany.greatapp", dispatch_queue_serial)   }    @objc func connect(resolver resolve: rctpromiseresolveblock, rejecter reject: rctpromiserejectblock) {     socket = gcdasyncudpsocket(delegate: self, delegatequeue: methodqueue())      //...start listening, etc   }    @objc func udpsocket(sock: gcdasyncudpsocket!, didreceivedata data: nsdata!, fromaddress address: nsdata!, withfiltercontext filtercontext: anyobject!) {     bridge.eventdispatcher().sendappeventwithname("got_msg", body: nil)   } } 

i've created private implementation

#import <foundation/foundation.h> #import "rctbridgemodule.h"  @interface rct_extern_module(mymodule, nsobject)  rct_extern_method(connect resolver:(rctpromiseresolveblock *)resolve                   rejecter:(rctpromiserejectblock *)reject)  @end 

however on occasion bridge.eventdispatcher() unwraps nil , unable broadcast event. thoughts appreciated.

this github issue led me solution: https://github.com/facebook/react-native/issues/3454. turns out happening on reload. 1 needs implement rctinvalidating , clean dangling references in invalidate. allows arc cleanup native module , reinstantiate rctbridge

@objc(mymodule) class mymodule: nsobject, gcdasyncudpsocketdelegate, rctinvalidating {   var bridge: rctbridge!   var socket: gcdasyncudpsocket!    func invalidate() {     self.socket = nil   }    func methodqueue() -> dispatch_queue_t {     return dispatch_queue_create("com.mycompany.greatapp", dispatch_queue_serial)   }    @objc func connect(resolver resolve: rctpromiseresolveblock, rejecter reject: rctpromiserejectblock) {     socket = gcdasyncudpsocket(delegate: self, delegatequeue: methodqueue())      //...start listening, etc   }    @objc func udpsocket(sock: gcdasyncudpsocket!, didreceivedata data: nsdata!, fromaddress address: nsdata!, withfiltercontext filtercontext: anyobject!) {     bridge.eventdispatcher().sendappeventwithname("got_msg", body: nil)   } } 

Comments

Popular posts from this blog

sql - invalid in the select list because it is not contained in either an aggregate function -

Angularjs unit testing - ng-disabled not working when adding text to textarea -

How to start daemon on android by adb -