Skip to content

Add operators to create Observables from BroadcastReceiver#1449

Closed
Yarikx wants to merge 2 commits into
ReactiveX:masterfrom
Yarikx:android-broadcasts
Closed

Add operators to create Observables from BroadcastReceiver#1449
Yarikx wants to merge 2 commits into
ReactiveX:masterfrom
Yarikx:android-broadcasts

Conversation

@Yarikx
Copy link
Copy Markdown
Contributor

@Yarikx Yarikx commented Jul 16, 2014

wraps BroadcastReceiver with Observable. Can be used with gloabl broadcasts and with local (using LocalBroadcastManager)

it allows to listen global and local (with support LocalBroadcastManager) broadcasts
@cloudbees-pull-request-builder
Copy link
Copy Markdown

RxJava-pull-requests #1399 FAILURE
Looks like there's a problem with this pull request

@Yarikx
Copy link
Copy Markdown
Contributor Author

Yarikx commented Jul 16, 2014

cast @mttkay.

There are no tests for OperatorBroadcastRegister yet, because Roboelectric seems to ignores calls
context.registerReceiver(broadcastReceiver, intentFilter, broadcastPermission, schedulerHandler), and I don't know how to write correct tests for it.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so one question I have about this is how it ties in with the Context life-cycle, e.g. for an Activity. Would I subscribe to the observable in onStart and unsubscribe on onStop (or other life-cycle pairs accordingly?)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also, this would mean that the context might get destroyed, and only then we post the call to unsubscribe to the message looper. We should test on a real device if this leads to problems e.g. during rotation changes and other instances of context loss

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that said, any reason why we can't do the unsubscribe synchronously? I don't think calls to unregisterReceiver have to happen on any particular thread

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

life-cycle management lies on the user. He/she must unsubscribe from observable (as with unregisterReceiver) to prevent context leak.

About unregistering in main thread you are right.
Just looked at the sources of unregisterReceiver. Probably there is no reason to unsubscribe in main thread.

@mttkay
Copy link
Copy Markdown
Contributor

mttkay commented Jul 16, 2014

This looks pretty good to me overall, would love to test it out.

One suggestion I have: have you thought about using Subjects instead? I think they would model more closely what a broadcast is, since they can fire without a subscriber.

@Yarikx
Copy link
Copy Markdown
Contributor Author

Yarikx commented Jul 16, 2014

I thought about it, but it creates much more questions, and become not so clear.

  1. It's more closer to "Producer" (I know that there is no such entity), than to Receiver.
  2. We can not determine (before registering receiver which type of Intent will we receive (sticky or not), so we can not pick up right implementation of subject (behavior or publish)
  3. And at least if user want to multicast observable, he/she can use operations like publish, replay, etc.
    He can even subscribe to observable several times (as it will create several independent Receivers)

@cloudbees-pull-request-builder
Copy link
Copy Markdown

RxJava-pull-requests #1400 SUCCESS
This pull request looks good

@dpsm
Copy link
Copy Markdown
Contributor

dpsm commented Jul 16, 2014

The intent matching within robolectric was fixed not too long ago here: robolectric/robolectric#1162

@Yarikx
Copy link
Copy Markdown
Contributor Author

Yarikx commented Jul 16, 2014

@dpsm I was having another problem.
As BroadcastOperator use call context.registerReceiver(broadcastReceiver, intentFilter, broadcastPermission, schedulerHandler) (event for method fromLocalBroadcast(Context context, IntentFilter filter)) it seems like roboelectric ignore this call at all.

However if i use context.registerReceiver(broadcastReceiver, intentFilter) everything works well in roboelectric, but I don't want to change semantics of code just to pass the tests.

@benjchristensen
Copy link
Copy Markdown
Member

Looks like there is still ongoing discussion here ... let me know when there is agreement on this being ready to merge.

@dpsm
Copy link
Copy Markdown
Contributor

dpsm commented Jul 22, 2014

@Yarikx I looked into the robolectric implementation of the Application shadow (https://github.com/xtremelabs/robolectric/blob/master/src/main/java/com/xtremelabs/robolectric/shadows/ShadowApplication.java) and indeed it does not implement the call you use to register the receiver. With that said, the test you have in the pull request fails?

Would you be interested in contributing to robolectric with a fix for it so the test can pass?

@Yarikx
Copy link
Copy Markdown
Contributor Author

Yarikx commented Jul 22, 2014

Yes. I would like.
Just let me day or two to setup environment, and start digging it.
On Jul 22, 2014 4:56 PM, "David Sobreira Marques" notifications@github.com
wrote:

@Yarikx https://github.com/Yarikx I looked into the robolectric
implementation of the Application shadow (
https://github.com/xtremelabs/robolectric/blob/master/src/main/java/com/xtremelabs/robolectric/shadows/ShadowApplication.java)
and indeed it does not implement the call you use to register the receiver.
With that said, the test you have in the pull request fails?

Would you be interested in contributing to robolectric with a fix for it
so the test can pass?


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

@Yarikx
Copy link
Copy Markdown
Contributor Author

Yarikx commented Jul 25, 2014

Actually I think it's already fixed in current version of robolectric (2.3) https://github.com/robolectric/robolectric/blob/afeb7321f3/src%2Fmain%2Fjava%2Forg%2Frobolectric%2Fshadows%2FShadowApplication.java, this project use 2.2.
I tried to increase version of it, but stuck with robolectric dependency to android-support lib, which gradle can not resolve. And actually I don't know how to resolve it without android gradle plugin.
Can anyone help with this test ?

@mttkay: If I will add tests for OperatorBroadcast, will it be candidate to be merged?

@mttkay
Copy link
Copy Markdown
Contributor

mttkay commented Jul 27, 2014

Sure.

By the way, anyone having problems building under IntelliJ lately? Something must have changed w.r.t. Gradle dependency configs, since anything in provided scope isn't recognized by IntelliJ as a compile time dependency anymore. I saw #1394 but it looks harmless to me, as it merely touches test dependencies?

@Yarikx
Copy link
Copy Markdown
Contributor Author

Yarikx commented Jul 29, 2014

Rebased it to #1528

@Yarikx Yarikx closed this Jul 29, 2014
mttkay added a commit to ReactiveX/RxAndroid that referenced this pull request Aug 19, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants