Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Background Listener is firing multiple times #341

Open
SamKissee opened this issue Oct 13, 2023 · 3 comments
Open

Background Listener is firing multiple times #341

SamKissee opened this issue Oct 13, 2023 · 3 comments
Labels
bug Something isn't working

Comments

@SamKissee
Copy link

Describe the bug
When attaching the background listeners, the console logs or any other action is fire 20+ times simultaneously

To Reproduce
Place useEffect with eventListener
Launch App
Go to Health and Add Data
View Response in Logs

Expected behavior
Listener should only fire once when new sample is received.

Smartphone (please complete the following information):

  • Device: iphone15 simulator and iphone 15 pro max physical device
  • OS: 17

Additional context
Located in our app container
React.useEffect(() => { new NativeEventEmitter(NativeModules.AppleHealthKit).addListener( 'healthKit:Running:new', async () => { console.log('--> Running observer triggered'); }, ); });

Data received from logs
LOG --> Running observer triggered LOG --> Running observer triggered LOG --> Running observer triggered LOG --> Running observer triggered LOG --> Running observer triggered LOG --> Running observer triggered LOG --> Running observer triggered LOG --> Running observer triggered LOG --> Running observer triggered LOG --> Running observer triggered LOG --> Running observer triggered LOG --> Running observer triggered LOG --> Running observer triggered LOG --> Running observer triggered LOG --> Running observer triggered LOG --> Running observer triggered LOG --> Running observer triggered LOG --> Running observer triggered LOG --> Running observer triggered LOG --> Running observer triggered LOG --> Running observer triggered LOG --> Running observer triggered LOG --> Running observer triggered LOG --> Running observer triggered

@SamKissee SamKissee added the bug Something isn't working label Oct 13, 2023
@iMonk777
Copy link

Hi @SamKissee , did you find any solution for this issue? We are facing the same thing.

@kyo504
Copy link

kyo504 commented Nov 20, 2023

@SamKissee , @iMonk777 After digging this, I found that the problem happens because startObserving function is called twice(one by initializeBackgroundObservers and the other when the first listener is added). IMO, if self.hasListeners is set to YES, we should ignore duplicate operation(which means adding observers).

I heard that there is massive factoring is on the way, so I'm not sure it's ok to submit a pr(maybe maintainers have already fixed this issue?!). Instead, if you want to fix this issue, you can apply the following simple patch using patch-package or yarn patch

-(void)startObserving {
    if (self.hasListeners) {
        return;
    }

    ...rest of your codes
}

@iMonk777
Copy link

Thanks @kyo504 , we will probably try this in a future release

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants