Suppose I have 3 student ids.
Things I want to do:
Fetch 3 students information asynchronously.
If callback not comes within 4 seconds it will retry for three times for that network call.
Here is my code:
Dictionary<string, SemaphoreSlim> semaphoreSlims = new Dictionary<string, SemaphoreSlim> { { "1", new SemaphoreSlim(0, 1) }, { "2", new SemaphoreSlim(0, 1) }, { "3", new SemaphoreSlim(0, 1) } }; IObservable<string> studentIds = new string[] {"1", "2", "3"}.ToObservable(); private IObservable<Student> LoadStudent(string id) { return Observable.Create<Student>( async o => { StudentFoundDelegate studentDetailHandler = (Student student) => { // callback found semaphoreSlims[id].Release(); o.OnNext(student); }; StudentNotFoundDelegate errorHandler = (string representation, Result eCode) => { // callback found semaphoreSlims[id].Release(); o.OnError(new Exception("Student Not Found")); }; // Api calls StudentManager.Instance.GetStudentDetail(id, studentDetailHandler, errorHandler); await semaphoreSlims[id].WaitAsync(); return Disposable.Empty; }); } Observable.Timeout(studentIds.SelectMany(id => LoadStudent(id)), TimeSpan.FromMilliseconds(4000)) .SubscribeOn(ThreadPoolScheduler.Default) .Retry(3) .Subscribe( student => { Logger.LogMessage("Found Student "+ student); }, (ex) => { Logger.LogMessage(ex + ""); } );
But, i am not getting any callbacks. Can anyone suggest where am i wrong and what is the proper way to handle it. Thanks.