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

bug: event listener is not notified even though event should be retained #2094

Closed
1 of 4 tasks
johan-mjolner opened this issue Oct 24, 2019 · 3 comments
Closed
1 of 4 tasks

Comments

@johan-mjolner
Copy link

johan-mjolner commented Oct 24, 2019

Bug Report

Calling a combination of addListener:, removeListener:, and notifyListeners:data:retainUntilConsumed: causes the notification to disappear

Capacitor Version

Version 1.2 but also latest version on Github as per 23/10/2019
npx cap doctor output:
💊 Capacitor Doctor 💊

Latest Dependencies:

@capacitor/cli: 1.2.1

@capacitor/core: 1.2.1

@capacitor/android: 1.2.1

@capacitor/ios: 1.2.1

Installed Dependencies:

@capacitor/cli 1.2.0

@capacitor/core 1.2.0

@capacitor/android 1.2.0

@capacitor/ios 1.2.0

[success] Android looking great! 👌
Found 2 Capacitor plugins for ios:
cordova-plugin-advanced-http (2.2.0)
cordova-plugin-file (6.0.2)
[success] iOS looking great! 👌

Affected Platform(s)

  • Android
  • iOS
  • Electron
  • Web

Current Behavior

I have created my own CAPPlugin subclass, where I call:

        self.notifyListeners("event", data: data, retainUntilConsumed: true)

At some point after this, my typescript files starts listening for "event":

        eventListener = Plugins.MyPlugin.addListener("event", ...)
        //...
        eventListener.remove()

This works the first time, I do this. But when repeating the same flow later, the listener is never called

Expected Behavior

I expect the listener to be called when I add the listener

Reproduction Steps

See "Current Behavior"

Resolution

The reason for the error is in CAPPlugin.m, the method "notifyListeners:data:retainUntilConsumed:". Here, there is a check for "listenersForEvent==nil". The problem is, that after adding and removing a listener, the "listenersForEvent" is not nill, but an empty array.

Please apply this pull request / this patch to fix the problem:

diff --git a/ios/Capacitor/Capacitor/CAPPlugin.m b/ios/Capacitor/Capacitor/CAPPlugin.m
index e3e1d202..5ec22f61 100644
--- a/ios/Capacitor/Capacitor/CAPPlugin.m
+++ b/ios/Capacitor/Capacitor/CAPPlugin.m
@@ -93,7 +93,7 @@ - (void)notifyListeners:(NSString *)eventName data:(NSDictionary<NSString *,id>

 - (void)notifyListeners:(NSString *)eventName data:(NSDictionary<NSString *,id> *)data retainUntilConsumed:(BOOL)retain {
   NSArray<CAPPluginCall *> *listenersForEvent = [self.eventListeners objectForKey:eventName];
-  if(listenersForEvent == nil) {
+  if(listenersForEvent == nil || [listenersForEvent count] == 0) {
     if (retain == YES) {
       [self.retainedEventArguments setObject:data forKey:eventName];
     }
@johan-mjolner johan-mjolner changed the title bug: bug: event listener is not notified even though event should be retained Oct 24, 2019
@johan-mjolner
Copy link
Author

I created a pull request for this: #2095

@jcesarmobile
Copy link
Member

closed on #2095

@ionitron-bot
Copy link

ionitron-bot bot commented Nov 13, 2022

Thanks for the issue! This issue is being locked to prevent comments that are not relevant to the original issue. If this is still an issue with the latest version of Capacitor, please create a new issue and ensure the template is fully filled out.

@ionitron-bot ionitron-bot bot locked and limited conversation to collaborators Nov 13, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants