question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Third observer to subscribe gets no events

See original GitHub issue

I’ve implemented an observer type to use with the AsObservable function. This works fine for the first two instances, but the third instance of the observer that I create never gets any events.

My observer class:

public class CommandObserver : IObserver<FirebaseEvent<Command>>
    {
        private IDisposable _unsubscriber;
        private IObservable<FirebaseEvent<Command>> _provider;

        public delegate void CommandChangedEventHandler(FirebaseEvent<Command> e);
        public event CommandChangedEventHandler CommandChanged;

        public delegate void CommandErrorEventHandler(Exception x, string userId);
        public event CommandErrorEventHandler CommandError;

        public delegate void ObjectCompletedEventHandler(string userId);
        public event ObjectCompletedEventHandler CommandCompleted;

        public string UserID { get; set; }
        public bool Stopped
        {
            get
            {
                return _unsubscriber == null;
            }
        }

        public CommandObserver(IObservable<FirebaseEvent<Command>> provider, string userId)
        {
            this.UserID = userId;
            _provider = provider;
        }

        public void Subscribe()
        {
            _unsubscriber = _provider.Subscribe(this);
        }

        public void Unsubsribe()
        {
            if(_unsubscriber != null) _unsubscriber.Dispose();
        }

        public void OnCompleted()
        {
            Debug.WriteLine($"CommandObserver completed for user {this.UserID}");
            this.Unsubsribe();
            this.CommandCompleted?.Invoke(this.UserID);
        }

        public void OnError(Exception error)
        {
            Debug.WriteLine($"CommandObserver error for user {this.UserID}: {error}");
            this.Unsubsribe();
            this.CommandError?.Invoke(error, this.UserID);
        }

        public void OnNext(FirebaseEvent<Command> value)
        {
            Debug.WriteLine($"Command {value.Key} changed on user {value.Object.UserID}");
            this.CommandChanged?.Invoke(value);
        }
    }

Where I get it:

public CommandObserver GetCommandObserver(string userId)
{
    var client = GetClient("DATABASE_NAME_HERE");
    var provider = client.Child("Commands").Child(userId).AsObservable<Command>();
    var observer = new CommandObserver(provider, userId);
    return observer;
}

I always call Unsubscribe on the previous observer before getting the next one, but it never fails - the third one will get no events.

I’m hoping someone can point towards what I’m guessing is an error on my part…

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
C0de1ove1029commented, Oct 25, 2018

Third subscriber doesn’t work here as well.

1reaction
JesperNJessencommented, Oct 4, 2018

Update: It seems there is simply a delay of about 10 seconds from subscribing the third observer until the events start coming through. The amount of data is miniscule so I’m thinking this is a limitation of either the REST API or “firebasedatabase.net”

Read more comments on GitHub >

github_iconTop Results From Across the Web

Observer subscribe not firing on first load to Component
1 Answer 1 ... Sequence of events is as follows (confirmed during debugging): ... It's because the vanilla Subject doesn't buffer anything. When...
Read more >
Understanding, creating and subscribing to observables in ...
It's good to know that when you subscribe to an observer, each call of subscribe() will trigger it's own independent execution for that...
Read more >
Using observables to pass values
When a second observer subscribes, the observable then wires up a new event handler and delivers values to that second observer in a...
Read more >
Angular Basics: Introduction to Observables (RxJS)—Part 2
We let an observable know that we want to receive data by calling its subscribe() method and passing in an observer object. The...
Read more >
Observable
Subscribing to an Observable is like calling a function, providing callbacks where the data will be delivered to. This is drastically different to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found