The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

BalrajS (2) [Avatar] Offline
#1
Under 5.3 section you have mentioned a interesting case study of periodically pulling data from a web service to show latest information. This kind of cases are mostly seen as Push mechanism is not implemented always from server end. Can you please elaborate on this case study with following points:
1. Start and stop polling based on internet connection
2. How to handle error without breaking Observable pipeline flow. As web services tend to throw many kinds of error
3. If multiple calls have gone to server due to less polling time. How to take response with latest request and skip older request data
4. How to synchronize observers subscription over multiple data pushed in Observable pipeline in parallel

Also if possible please incorporate some other case studies like these. This will help us to understand and think more from implementation point in day to day programming.

Till now this book has been great source of learning material for Rx. Eagerly waiting for upcoming chapters.
Tamir Dresher (35) [Avatar] Offline
#2
Hi BalrajS
Thanks for the feedback!

1. in chapter 6 i'm going deeper into the observable-observer relationship and shows different ways to control when to start and stop subscribing to the observable or observing the notifications, specifically you might be interested in the TakeUntil and SkipUntil operators to control the observations based on external triggers (like the connectivity)
2. error handling is disucssed in chapter 11, until it's published, you can try to read about the Catch operator or the Retry operator
3. the Switch operator (introduced in chapter 5) might be what youre looking for
4. chapter 10 talks about synchronization between observables (or withing a single observable). i also showed an example in chapter 2 of using the Synchronize operator. Note, that synchronization might make your observable-pipelines end in a deadlock (like with all types of synchrnoization) so use with care


Writing about practical usages is our main goal with this book, i'm glad you liked it and i'll do my best to add more examples like this.

Hope that i helped
Thanks
Tamir
BalrajS (2) [Avatar] Offline
#3
For point no 3 "If multiple calls have gone to server due to less polling time. How to take response with latest request and skip older request data". How will the switch operator help?

Following is the code attached in which I need to print only numbers in increasing order. Means latest response that is generated from asyncCall sequence should be considered and any order request made to asyncCall should be ignored in all cases. How can I achieve that?


      private static void GenerateObservableSequence()
        {
            var timerData = Observable.Timer(TimeSpan.Zero,
                TimeSpan.FromSeconds(1));

            var asyncCall = Observable.FromAsync<int>(() =>
            {
                TaskCompletionSource<int> t = new TaskCompletionSource<int>();
                i++;

                int k = i;
                var rndNo = new Random().Next(3, 10);
                Task.Delay(TimeSpan.FromSeconds(rndNo)).ContinueWith(r => { t.SetResult(k); });
                return t.Task;
            });

            var obs = from t in timerData
            from data in asyncCall
            select data;

            var hot = obs.Publish();
            hot.Connect();
                
                hot.Subscribe(j => 
            {
                Console.WriteLine("{0}", j);
            });
        }
Tamir Dresher (35) [Avatar] Offline
#4
consider this example, where i use some async method that simulates the request to the server (and returns Task<TResult>smilie

Observable.Interval(TimeSpan.FromSeconds(2))
.Select(_=> PullDataFromServerAsync())
.Switch()
.Subscribe(/*processing results*/);


in this case, if for one of the data pulling it took longer than two seconds to return (a case that we consider rare), then another request will be sent to the server because the Interval operator already emitted another notification.
The Switch operator will stop waiting to the result from the previous request, and will Switch to the new request.

of course, if for every 'iteration' the time it takes to recieve the results is longer than the time-interval, we wond ever get results. it's up to you to decide what should be done in this case.

BTW, in the example you provided, it will be easier to write the asyncCall as a simple method that return a Task, or if you really want, just provide an async lambda expression to the FromAsync method. and await the Task.Delay(). this way you wont need to use the TaskCompletionSource