step-up-labs / firebase-database-dotnet

C# library for Firebase Realtime Database.
MIT License
671 stars 170 forks source link

Stop receiving updates for old items using the StreamingOptions.LatestOnly parameter if you close and open the application #258

Closed erickvrech89 closed 2 years ago

erickvrech89 commented 3 years ago

I'm using Xamarin.Forms, I have a singleton 'service' and in the contructor I connect using the code below and await the subscribe event.

_firebase = new FirebaseClient("https://X.firebaseio.com",
    new FirebaseOptions
    {
        OfflineDatabaseFactory = (t, s) => new OfflineDatabase(t, s)
    });

_shopDb = _firebase.Child(nameof(Shop).ToLower()).AsRealtimeDatabase<Shop>("", "", StreamingOptions.LatestOnly, InitialPullStrategy.MissingOnly, true);
_shopDb.AsObservable().Subscribe(item =>
    Console.WriteLine($"Firebase Subscribe: {item.EventType} {item.EventSource}"));

However, when connecting normally, I go to the firebase portal and manually add one or two items, the application normally receives the inserted items, if I change both items manually through the portal, the application continues to receive updates, however if I disconnect and connect the device's internet or restarting the app, I don't get any more updates, just new items inserted, if I change the old items I don't get it but if I change the new one I normally receive it, however, if I restart the internet or the app again problem happens with the new item and I never get updates from it again, but everyone is offline but with the last value received by the 'subscribe' before I restart the internet of the device or the app.

Using the parameter 'StreamingOptions.Everything' the problem does not occur when reconnecting the internet or restarting the app, but I don't want to use it because I get the items from the online collection every time I reopen the app even though I am already offline correctly, consuming the bandwidth download of the paid plan immensely of Firebase.

From what I tested locally, the parameter 'StreamingOptions.LatestOnly' is also working correctly until you restart the app or reconnect the device's internet, so I'm in doubt if there are any bugs or optimization to be done in the library.

I hope I have summarized it well, and sorry for the bad English

Thnaks.

VikashSinghRathore commented 3 years ago

I had the same problem few months back. After searching Internet I got a fix.

we have to create a IDisposable variable out of subscribe function like this

IDisposable Subscriber = _shopDb.AsObservable().Subscribe(item => Console.WriteLine($"Firebase Subscribe: {item.EventType} {item.EventSource}"));

now Whenever you need to close your application or do anything which needs to destroy the object, just dispose this "Subscriber" Object.

Subscriber.Dispose();

and you are good to go.

Actually the problem is that we have to properly Detach listeners which is listening to the node at Realtime. you can find more details from this page https://firebase.google.com/docs/database/web/lists-of-data#detach_listeners

stale[bot] commented 2 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 2 years ago

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