Skip to content
This repository has been archived by the owner on Apr 30, 2019. It is now read-only.

Events with subclasses #122

Open
vpratfr opened this issue Jul 27, 2014 · 6 comments
Open

Events with subclasses #122

vpratfr opened this issue Jul 27, 2014 · 6 comments

Comments

@vpratfr
Copy link

vpratfr commented Jul 27, 2014

Hi,

first, thanks for this library. It's great. I have a question, I don't know whether this is intentional or a bug.

My Event hierarchy:

class A {
  public static class A1 extends A {}
  public static class A2 extends A {}
}

My producer:

class Activity {
  @Produce 
  public A1 initialA1Event() { return new A1; }

  // Register in onCreate, unregister in onDestroy
}

My receiver:

class Fragment {

  // Never gets the initial A1 event from activity
  @Subscribe
  public void receiveAnyA(A event) {}

  // Register in onResume, unregister in onPause
}

With the above code, the subscriber does not receive an initial A event which would be produced by the Activity. If I change the subscribe handler to receive A1 specifically, it works.

I would have expected that my Fragment gets notified of the activity's A1 as an A event.

@JakeWharton
Copy link
Collaborator

Producers don't honor class hierarchy like posting does. What would happen
if you were producing both A1 and A2?
On Jul 27, 2014 2:55 AM, "Vincent Mimoun-Prat" [email protected]
wrote:

Hi,

first, thanks for this library. It's great. I have a question, I don't
know whether this is intentional or a bug.

My Event hierarchy:

class A {
public static class A1 extends A {}
public static class A2 extends A {}
}

My producer:

class Activity {
@produce
public A1 initialA1Event() { return new A1; }

// Register in onCreate, unregister in onDestroy
}

My receiver:

class Fragment {

// Never gets the initial A1 event from activity
@subscribe
public void receiveAnyA(A event) {}

// Register in onResume, unregister in onPause
}

With the above code, the subscriber does not receive an initial A event
which would be produced by the Activity. If I change the subscribe handler
to receive A1 specifically, it works.

I would have expected that my Fragment gets notified of the activity's A1
as an A event.


Reply to this email directly or view it on GitHub
#122.

@vpratfr
Copy link
Author

vpratfr commented Jul 27, 2014

What would happen if you were producing both A1 and A2?

I would have expected in that case that both A1 and A2 get delivered to the subcribers of A. In fact I would expect the producers to simply have the same behaviour as when using posting.

@JakeWharton
Copy link
Collaborator

That seems illogical and doesn't scale to many subclasses. If you want a
produced value for A you should produce it directly and reconcile the many
different subclasses into a single value. Otherwise it sounds like you'd
want a different mechanism for seeding initial state.
On Jul 27, 2014 3:07 PM, "Vincent Mimoun-Prat" [email protected]
wrote:

What would happen if you were producing both A1 and A2?

I would have expected in that case that both A1 and A2 get delivered to
the subcribers of A.


Reply to this email directly or view it on GitHub
#122 (comment).

@alexustinovsm
Copy link

For instance,

public class A {
  // This code will work
  @Subscribe public onSomeEvent(SomeEvent event) { /* my code here */ }
}

// This code won't work because HandlerFinder doesn't find subscribe
public class A1 extends A {}

// This code won't work because HandlerFinder doesn't find subscribe
public class A2 extends A {}

I think this is a bug. I don't want to create the same method at 10 different children of A. I think it's very strange.

@sferra
Copy link

sferra commented May 10, 2015

@alexustinovsm While the issue you describe doesn't relate to producers, I must agree that it is buggy behavior. The workaround would currently be to explicitly implement onSomeEvent in both A1and A2 so the methods are found:

public class A1 extends A {
  @Subscribe public onSomeEvent(SomeEvent event) {
    super.onSomeEvent(event);
  }
}

public class A2 extends A {
  @Subscribe public onSomeEvent(SomeEvent event) {
    super.onSomeEvent(event);
  }
}

Maybe open a separate ticket for this issue?

@adrien-aubel
Copy link

I agree with @sferra, this is a behavior that is surprising. I think this should be at least mentioned in the documentation.

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

5 participants