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

ConcurrentModificationException in NotificationCenter.publish #289

Closed
mgznt opened this issue Jul 31, 2015 · 2 comments
Closed

ConcurrentModificationException in NotificationCenter.publish #289

mgznt opened this issue Jul 31, 2015 · 2 comments

Comments

@mgznt
Copy link

mgznt commented Jul 31, 2015

When trying to subscribe to events during a call to NotificationCenter.publish(), you get a ConcurrentModificationException from the subscribers list.

One solution would be to make a copy of the subscribers list before publishing. Here's a minimal test case (tested with mvvmFx 1.3.1):

import org.junit.Test;

import de.saxsys.mvvmfx.utils.notifications.NotificationCenter;
import de.saxsys.mvvmfx.utils.notifications.NotificationCenterFactory;

public class NotificationCenterTest {
  @Test
  public void shouldAllowSubscribeDuringPublish() throws Exception {
    NotificationCenter c = NotificationCenterFactory.getNotificationCenter();

    /* Minimal example (triggers a ConcurrentModificationException in NotificationCenter */
    c.subscribe("MSG", (key1, payload1) -> {
      c.subscribe("MSG", (key2, payload2) -> {
        System.out.println("I want to subscribe during a publish()");
      });
    });

    c.publish("MSG");

    /*
     * Real use case:
     * 
     *      ParentView
     *       |       |
     * ChildView1  ChildView2
     * 
     *  
     * ChildView1 publishes a global event that causes ParentView
     * to create a new instance of ChildView2.
     * ChildView2 tries to subscribe to another global event in its initialize() method.
     * 
     * Because NotificationCenter.publish() does not make a copy before publishing
     * events, we get a ConcurrentModificationException. 
     */
  }
}
manuel-mauky added a commit that referenced this issue Jul 31, 2015
…ntModification

#289 fix ConcurrentModificationException in NotificationCenter
@manuel-mauky
Copy link
Collaborator

Thanks for your report. I've created a PR (#290) that fixes the problem and makes your test case green.
You can test it with the 1.4.0-SNAPSHOT.

@mgznt
Copy link
Author

mgznt commented Jul 31, 2015

Wow, that was quick! Thanks 👍

@mgznt mgznt closed this as completed Jul 31, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants