Reputation: 15929
I'm observing a database table wit RxJava. All SQL SELECT query return an hot observable and whenever I INSERT / UPDATE / DELETE rows in the same table I will rerun any subscribed SELECT query and call onNext(queryResult)
on the hot query observable.
database.select("SELECT * FROM Foo) // returns an observable
.map(doSomething)
.filter(aFilter)
.subscribe (
{
// onNext
},
{
// onError
}
)
This works quite well. However, in one single special use case I want to avoid that the database is re-emitting a new item. Is there some kind of operator I can use to make it a single "cold" observable without having to change all of my database layer which is build for hot observables like this:
database.select("SELECT * FROM Foo) // returns an observable
.map(doSomething)
.filter(aFilter)
.toColdObservable()
.subscribe (
{
// onNext
},
{
// onError
}
)
I know "cold observable" is not the right word for that, but I haven't found a better name. So what I want is that the database don't notify the SQL Query Observable about an update. So what I mean with "cold observable" is just querying the database one time and not receiving updates afterwards anymore.
Upvotes: 2
Views: 4174
Reputation: 2247
How about simple take(1)
? This will complete your query observable after first result.
Upvotes: 6
Reputation: 31339
If you don't want the notification you can use an empty subscribe which effectively ignores the results:
Subscribes to an Observable but ignore its emissions and notifications.
database.select("SELECT * FROM Foo") // returns an observable
.map(doSomething)
.filter(aFilter)
.subscribe ()
Upvotes: 1
Reputation: 69997
There are two main ways for converting a hot observable into a cold one: defer
to defer its existence entirely or replay
+ autoConnect(0)
to pre-run it and just replay to others as a cold one.
However, the notification scheme of this select
means it won't ever complete so you need some kind of timeout
applied so the replay is bounded in size.
I'd also check if the library actually supports one-time run (cold) queries where you don't have this problem; each subscriber will run a new and limited query. Otherwise, that could be a nice feature request.
Upvotes: 1