0

Suppose I have 3 student ids.

Things I want to do:

  1. Fetch 3 students information asynchronously.

  2. 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.

Community
  • 1
  • 1
user246186
  • 39
  • 4
  • 1
    If ever you find yourself writing `return Disposable.Empty;` then you are doing something wrong. – Enigmativity Jan 01 '19 at 11:23
  • You really need to provide a [mcve]. I need to be able to copy, paste, and run your code in a console app to effectively fix your code. – Enigmativity Jan 01 '19 at 11:25

0 Answers0