question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Possible issue with using ReplayingShare to share connections

See original GitHub issue

This is more of a question or discussion, not an issue, since it doesn’t relate to any of the RxAndroidBle code. However, it does relate to the recommendation to use ReplayingShare instead of the ConnectionShareAdapter.

This is the issue.

  • A device was just scanned.
  • The device is recognized and grabbed.
  • Then establishConnection is called on this device and shared through the use of ReplayingShare. The resulting shared Rx observable is cached in the code that grabbed the device.
  • Say an observer subscribes to the above shared Rx observable and then flat-maps with a connection.readCharacteristic. All goes well, data is read hand handled.
  • Then there is an issue with the Bluetooth adapter or reception, but it is quickly resolved. However, the current connection is no longer valid. It is stale, the device is disconnected. The current observer receives an onError and handles the disconnection error.
  • A little later a second observer subscribes to the above shared Rx observable.
  • Since this shared Rx observable uses the ReplayingShare, the second subscription causes an invalid/stale connection to be emitted by the ReplayingShare.

The problem is that ReplayingShare does not react to finalizations (onComplete, onError or onDispose). This means that ReplayingShare cannot be used to share resources that become invalid/stale as soon as subscriptions to these resources are finalized. The RxBleConnection is such a resource.

In our project, I worked around this by copying the ReplayingShare code and add a little bit of code: In the two apply methods of the ReplayingShare, I changed this

  • upstream.doOnNext(lastSeen).share() to this
  • upstream.doFinally { lastSeen.value = null }.doOnNext(lastSeen).share()

This prevents invalid/state RxBleConnections to be emitted and solved the issues we had with concurrent connections.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:21 (3 by maintainers)

github_iconTop GitHub Comments

6reactions
RobLewiscommented, Apr 8, 2018

I discussed this with Jake Wharton and he agrees there’s a problem. He’s mulling over the solution.

2reactions
RobLewiscommented, May 18, 2018

I touched base with Wharton recently and he said that he should soon have time to work on this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Possible issue with using ReplayingShare to share connections
This is the issue. A device was just scanned. The device is recognized and grabbed. Then establishConnection is called on this device and...
Read more >
RXAndroidBLE: Compile error (@ReplayingShare)
I tried to implement the method 2 of sharing a BLE connection. Whatever I tried, it's not compiling. I'm new to Java, Android,...
Read more >
RxBLELibraries/RxAndroidBle - Gitter
I have used ReplayingShare, could that be the problem that caused GATT 133 ... and if connection is lost, I am using ReplayingShare...
Read more >
com.jakewharton.rx : replaying-share-parent : 1.0.1 - Maven Central ...
RxJava Replaying Share (Parent) - An RxJava transformer which combines replay(1), publish(), and refCount() operators.
Read more >
setupNotification returns "Error already connected" whereas ...
Coding example for the question setupNotification returns "Error already connected" whereas no connection request is send-kotlin.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found