title: React Native and Objective C delegates
body: <p>I am quite new to react native and and the bridging mechanism with native code, especially when the framework has delegates. Assume I am trying to bridge the following framework:</p>
  
  <pre><code>@protocol BRPtouchNetworkDelegate;
  
  @class PLNetworkModule;
  
  @interface BRPtouchNetworkManager : NSObject &lt;NSNetServiceBrowserDelegate,NSNetServiceDelegate&gt;
  
  
  @property(retain, nonatomic) NSMutableArray* registeredPrinterNames;
  @property(assign, nonatomic) BOOL isEnableIPv6Search;
  
  
  - (int)startSearch: (int)searchTime;
  - (NSArray*)getPrinterNetInfo;
  
  - (BOOL)setPrinterNames:(NSArray*)strPrinterNames;
  - (BOOL)setPrinterName:(NSString*)strPrinterName;
  
  - (id)initWithPrinterNames:(NSArray*)strPrinterNames;
  - (id)initWithPrinterName:(NSString*)strPrinterName;
  
  @property (nonatomic, assign) id &lt;BRPtouchNetworkDelegate&gt; delegate;
  
  @end
  
  @protocol BRPtouchNetworkDelegate &lt;NSObject&gt;
  
  -(void) didFinishSearch:(id)sender;
  
  @end
  </code></pre>
  
  <p>The following is the bridge module I implemented:</p>
  
  <p>RCTBRPtouchNetworkManager.h</p>
  
  <pre><code>#import &lt;React/RCTBridgeModule.h&gt;
  #import &lt;BRPtouchPrinterKit/BRPtouchPrinterKit.h&gt;
  
  @interface RCTBRPtouchNetworkManager : NSObject &lt;RCTBridgeModule, BRPtouchNetworkDelegate&gt;
  @end
  </code></pre>
  
  <p>RCTBRPtouchNetworkManager.m</p>
  
  <pre><code>#import "RCTBRPtouchNetworkManager.h"
  #import &lt;BRPtouchPrinterKit/BRPtouchPrinterKit.h&gt;
  #import &lt;React/RCTLog.h&gt;
  
  @implementation RCTBRPtouchNetworkManager {
    BRPtouchNetworkManager  *_networkManager;
  }
  
  RCT_EXPORT_MODULE();
  
  RCT_EXPORT_METHOD(addEvent:(NSString *)name location:(NSString *)location)
  {
    RCTLogInfo(@"Pretending to create an event %@ at %@", name, location); //a dummy method to test the bridge
  }
  
  RCT_EXPORT_METHOD(startSearchWithTimeout:(int)time) {
    RCTLogInfo(@"Bridge started search with time %d", time);
  
    _networkManager = [[BRPtouchNetworkManager alloc] init];
    _networkManager.delegate = self; //I'm setting delegate here
    _networkManager.isEnableIPv6Search = NO;
    NSString *  path = [[NSBundle mainBundle] pathForResource:@"PrinterList" ofType:@"plist"];
    if( path )
    {
      NSDictionary *printerDict = [NSDictionary dictionaryWithContentsOfFile:path];
      NSArray *printerList = [[NSArray alloc] initWithArray:printerDict.allKeys];
      [_networkManager setPrinterNames:printerList];
    } else {
      RCTLogInfo(@"PrinterList path not found");
    }
  
    //  Start printer search
    [_networkManager startSearch: 5.0];
  
  }
  
  - (void)didFinishSearch:(id)sender {
    NSLog(@"didFinishedSearch"); //this delegate method is not called
  }
  
  @end
  </code></pre>
  
  <p>I can easily call the dummy method and see the results in the logs. However, the delegate method didFinishSearch() is never called. I call this from javascript as follows:</p>
  
  <pre><code>componentDidMount() {
      let networkManager = NativeModules.BRPtouchNetworkManager;
      networkManager.startSearchWithTimeout(5.0);
  }
  </code></pre>
  
  <p>I there something I am missing? Am I implementing delegate properly? Is this kind of functionality even possible (can't seem to not since the delegate method was used by iOS community for a long time). Your help is much appreciated. </p>
  
  <p><strong>EDIT</strong></p>
  
  <p>I found that adding the following to my bridge manager file made the delegate to fire (thanks to <a href="https://stackoverflow.com/questions/36747333/eademo-in-react-native-module-never-receives-delegate-method-handleevent-nsstrea">this post</a>)</p>
  
  <pre><code>- (dispatch_queue_t)methodQueue
  {
      return dispatch_get_main_queue();
  }
  </code></pre>
  
  <p>However, even though this solves the problem, I'd like a more technical understanding on what is going on here since I can't seem to exactly grasp it. Thank you</p>