Open jasonmerino opened 7 years ago
First off, I really like the idea of a generic addListener
method, that would simplify adding future events significantly!
I haven't dealt with this myself, but from the docs, it seems like the best way would be to use a RCTEventEmitter
class and override the startObserving
and stopObserving
methods. https://facebook.github.io/react-native/docs/native-modules-ios.html#optimizing-for-zero-listeners
That should allow you to stop the listeners when the last JS listener is removed.
I've been working on a
Lookback.addListener
method for this library which would allow the consumer to subscribe to recording status changes (basically exposing changes in theisRecording
getter). I chatted with someone from Lookback.io and they mentioned that I could use KVO to listen for those changes. Unfortunately, all of the examples I've seen for using KVO are done in an Objective-C view controller, which is problematic for using KVO in React Native because I'm not sure when to unobserve the values changes.I have successfully added the KVO functions to listen for a change in
isRecording
and was able to get them to call the callback function I passed toLookback.addListener
with the recording status. But I don't know how the best way to stop the KVO in order to free up that Objective-C listener.I was curious if anyone could give me some information on how best to do this in React Native.
My ideas so far are to hook into an app status event (when the user backgrounds or closes the app) and stop the KVO then. Or, instead of attaching a listener to the
Lookback
module I was thinking I could just pass an optional function to thestartWithAppToken
method which would start the KVO and then stop it when the user stops the recording. Both of these seem less than ideal, but I'm having trouble wrapping my mind around another way.