negativetwelve / react-native-lookback

React Native module for Lookback.io
MIT License
13 stars 7 forks source link

[iOS] Add event listener for recording status change #24

Open jasonmerino opened 7 years ago

jasonmerino commented 7 years ago

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 the isRecording 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 to Lookback.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 the startWithAppToken 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.

negativetwelve commented 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.