darryncampbell / react-native-datawedge-intents

React Native interface for Zebra's DataWedge Intent API
MIT License
43 stars 46 forks source link

registerBroadcastReceiver problem #10

Closed andreibursuc97 closed 3 years ago

andreibursuc97 commented 3 years ago

Looks like on app reload every time I call registerBroadcastReceiver a new receiver would be created and the old one will not be unregistered. So when I first open the app everything works as expected and I receive an intent per scan, but after I reload the app the registerBroadcastReceiver is called again and from now on I will receive two intents per scan. If I reload the app again I will receive three intents per scan and so on. Is there a way to unregister the receiver when the react component is destroyed so this problem won't appear anymore?

andreibursuc97 commented 3 years ago

Ok, so the fix is fairly simple, the genericReceiver object needs to be made static, because if it is not static, at refresh, a new object (with no reference to the old one) will be created and the unregisterReceiver method that is called before a new receiver is registered will consider that this new created object was not registered before so there is nothing to unregister.

darryncampbell commented 3 years ago

Thanks @andreibursuc97 , I don't like static methods as a rule but the fix seems to work and I do not have a better solution... request you to please review https://github.com/darryncampbell/react-native-datawedge-intents/pull/11 before I merge to master and publish a new version of this plugin to npm. Thanks.

darryncampbell commented 3 years ago

Resolved by https://github.com/darryncampbell/react-native-datawedge-intents/pull/11 and published as 0.1.3, https://www.npmjs.com/package/react-native-datawedge-intents/v/0.1.3