swiftreact-nativereact-native-native-module

How to extend RCTEventEmitter while overriding methodQueue getter?


I'm trying to create a React Native native module in Swift that needs to be run on the main thread and which sends events to JS. This works fine (as instructed in RN docs):

@objc(MyModule)
class MyModule: NSObject {
  @objc
  func methodQueue() -> DispatchQueue {
    return DispatchQueue.main
  }
}

The recommended way to send events is to extend RCTEventEmitter. When I change NSObject to RCTEventEmitter in the above code, I get the error:

Method 'methodQueue()' with Objective-C selector 'methodQueue' conflicts with getter for 'methodQueue' from superclass 'RCTEventEmitter' with the same Objective-C selector

How can I override the methodQueue getter while at the same time extending RCTEventEmitter? Or is there some other way to send events to JS?


Solution

  • You need to override it as a var getter:

      @objc
      override var methodQueue: DispatchQueue {
        get {
          return DispatchQueue.main
        }
      }