Note that there are some explanatory texts on larger screens.

plurals
  1. POCreating an observable which relies on state from an Interval
    text
    copied!<p>I am trying to write a class which will query a set of data out of a database every x seconds.</p> <p>The issue is that this observable remembers it's previous state and that state is used to determine what is actually sent on the observable.</p> <p>For Example I want an </p> <pre><code>IObservable&lt;SomeEntity&gt; AddedEntities { get; private set; } IObservable&lt;SomeEntity&gt; ChangedEntities { get; private set; } IObservable&lt;SomeEntity&gt; DeletedEntities { get; private set; } </code></pre> <p>My issue is that I have been reading <a href="http://www.introtorx.com/" rel="nofollow">http://www.introtorx.com/</a> and they say that using Subject is a bad idea. Instead I should be using the Observable.Create methods.</p> <p>My issue is I only want to query the database once and then send the relevant information back to the correct observable but I dont want each subscribe to my observable start it's own timer and query the database every 5 seconds per subscription to each IObservable.</p> <p>I have played around with making just one observable which returns a new model with the changed as a set:</p> <pre><code>IObservable&lt;EntityChangeSet&gt; Changes {get; private set;} public class EntityChangeSet { Public IEnumerable&lt;SomeEntity&gt; Added {get; set;} Public IEnumerable&lt;SomeEntity&gt; Changed {get; set;} Public IEnumerable&lt;SomeEntity&gt; Deleted {get; set;} } </code></pre> <p>And I am open to any solution like this as well.</p> <p>What I have so far is:</p> <pre><code>public class IntervalChangeReader { // My state between ticks. private IEnumerable&lt;SomeEntity&gt; knowEntities; // Reads the data from the db and uses knowEntities to determine adds, changes, // and deletes which are exposed through properties. private DbReaderAndChangeChecker checker; IDisposeable timerCancel; public IntervalChangeReader(DbReaderAndChangeChecker checker) { this.checker = checker; } public IObservable&lt;EntityChangeSet&gt; Changes { get; private set; } public Start(int seconds) { this.timerCancel = Observable.Interval(new TimeSpan.FromSeconds(seconds)).Subscribe( x =&gt; { var results = this.checker.Refresh(this.knownEntities); // Update this.knownEntities with results. // Inform produce a value on the observable ???? // I could call .OnNext If I had a subject exposed for my Observable. } } public Stop() { this.timerCancel.Dispose(); // Complete on all subscriptions? // If I were using subjects I could just call .OnComplete here. } } </code></pre> <p>How do I create the Observable without using subjects and how to I pass results through it? I want to do this all with only one timer and not a timer per subscription to my observables.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload