I have the following class:
#import "EventHandler.h"
#import "RCTBridge.h"
#import "RCTEventDispatcher.h"
#import <pthread.h>
@implementation EventHandler
RCT_EXPORT_MODULE();
@synthesize bridge = _bridge;
RCT_EXPORT_METHOD(addEvent:(NSString *)name location:(NSString *)location)
{
mach_port_t machTID = pthread_mach_thread_np(pthread_self());
NSLog(@"Pretending to create an event %@ at %@, current thread: %x", name, location, machTID);
[self updateLocationEvent];
}
- (void)updateLocationEvent
{
NSString *eventName = @"name!!!";
mach_port_t machTID = pthread_mach_thread_np(pthread_self());
NSLog(@"about to submit event, current thread: %x",machTID);
[self.bridge.eventDispatcher sendAppEventWithName:@"UpdateLocation"
body:@{@"name": eventName}];
}
@end
My problem: I'm trying with no success to run updateLocationEvent
method from main thread method
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
Which (for those who are not familiar with it) is a method that tracks location whenever there's a significant change. Once inside the updateLocationEvent
method while calling from didUpdateLocations
the property self.bridge
is nil
! If you take a look at the other method RCT_EXPORT_METHOD(addEvent:(NSString *)name location:(NSString *)location)
I also call updateLocationEvent
from there and it emits the event just fine. Actually I just created the method to test if the event was getting fired at all and it did. The class that handles didUpdateLocations
has an EventHandler
property which is initialized the same way I've seen people do in objective-c:
if (_eventHandler == nil) {
_eventHandler = [[EventHandler alloc] init];
}
[_eventHandler updateLocationEvent];
Perhaps I'm doing this wrong? I'm very new to obj-c and relatively new to react but I just haven't found an example where I can execute an event emitting method from a native method so any help would be greatly appreciated.