question

RuudvanderLinden-0058 avatar image
0 Votes"
RuudvanderLinden-0058 asked RobCaplan edited

Firebase database offline

I'm using Firebase Database in my Xamarin Android app. It works fine, but I cannot get offline persistence to work. From what I'm reading I should just set SetPersistenceEnabled to true and KeepSynced to true on the database reference. Below my code for that part.

         public void Initialize()
         {
             FirebaseApp firebaseApp = FirebaseApp.InitializeApp(Application.Context);
             FirebaseDatabase.GetInstance(firebaseApp).SetPersistenceEnabled(true);
             firebaseDatabase = FirebaseDatabase.GetInstance(firebaseApp);
             firebaseAuth = FirebaseAuth.GetInstance(firebaseApp);
             firebaseStorage = FirebaseStorage.Instance;
    
             GetUser();
         }

         private async void OnLogin(LoginResultEventArgs e)
         {
             if (e.LoginResult == LoginResult.Succes)
             {
                 projectsReference= firebaseDatabase.GetReference(firebaseAuth.CurrentUser.Uid + "/projects");
                 projectsReference.KeepSynced(true);
                    
             }
         }

Initialize is called from OnResume in my Splash activity.

When offline it doesn't work. For example below code. SetValueAsync never returns.

         public async Task AddProject(FRTDBProject project)
         {
             var reference = projectsReference.Push();
             var id = reference.Key;
             project.Id = id;
             await reference.SetValueAsync(FRTDBProject.FRTDBProjectModelToMap(project));
         }

Anyone has some working Xamarin Android offline Firebase database example or perhaps knows what the problem is?

Thanks.








dotnet-xamarin
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi,RuudvanderLinden. Could the 'SetValueAsync' commmand save the data successfully? The Firebase Realtime Database client automatically keeps a queue of all write operations that are performed while your app is offline. When the app regains connectivity, all of the operations are sent to the Firebase Realtime Database server. It may affect the return value. Try to use the 'GetValueAsync' method to check if the data has been stored.

0 Votes 0 ·

Hi JarvanZhang,

Thanks for your reply.

SetValueAsync does indeed save the data successfully while offline. When I go online, even after restarting, the data that was saved offline is there.

But there are a couple of problems.

I await SetValueAsync in my code and while offline it never returns.

Second problem is that I use the AddListenerForSingleValueEvent on the reference to retrieve the data. But while offline the listener events are never called. Even OnCancelled(DatabaseError error) is not called.

So in short, saving while offline is fine, but my code doesn't retrieve the data offline.

Ruud

0 Votes 0 ·

1.When you add a single value event listener to the same location, the Firebase client will immediately invoke onDataChange() for the value from the local disk cache. It will not invoke the onDataChange() any more times, even if the value on the server turns out to be different.

Try to use addValueEventListener() instead of a single-value event listener. A regular value listener will get both the immediate local event and the potential update from the server.

2The onCancelled method will be triggered when you don't have permission. The client doesn't know whether you have permission (since it's not connected to the server), so it cannot invoke onCancelled either.


Check the links:
https://stackoverflow.com/a/34487195/11083277
https://stackoverflow.com/a/42431732/11083277

0 Votes 0 ·

1 Answer

RuudvanderLinden-0058 avatar image
1 Vote"
RuudvanderLinden-0058 answered

I've switched to AddValueEventListener() and SetValue() instead of SetValueAsync().

Everything is working as expected now.

Thanks for your help JarvanZhang.



5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.