step-up-labs / firebase-database-dotnet

C# library for Firebase Realtime Database.
MIT License
672 stars 168 forks source link

Third observer to subscribe gets no events #176

Closed JesperNJessen closed 4 years ago

JesperNJessen commented 6 years ago

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

JesperNJessen commented 6 years ago

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"

C0de1ove1029 commented 6 years ago

Third subscriber doesn't work here as well.

mkbg8 commented 5 years ago

In my case, the delay is 90s. Limitation or not, this is not working for me. Maybe somebody has any ideas... As posted to another instance of this issue, it seems SendAsync in FirebaseSubscription.ReceiveThread is taking a very very long time (i.e. the var response = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, this.cancel.Token).ConfigureAwait(false); line).

stale[bot] commented 4 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] commented 4 years ago

Closing the issue due to inactivity. Feel free to re-open