TobiasBuchholz / Plugin.Firebase

Wrapper around the native Android and iOS Firebase Xamarin SDKs
MIT License
211 stars 49 forks source link

Firestore freeze when offline #274

Closed petrdrabek closed 3 months ago

petrdrabek commented 6 months ago

Hi! I encoutered a freezing problem when using firebase firestore without internet connection. Every add/update/remove operation on document/collection does not return until internet is available again. Due to that app will freeze.

If operation is not awaited, document is added/updated correctly in local cache, but not awaiting theese operations is not a good idea.

Also get operations are too slow when offline, but at least they work.

TobiasBuchholz commented 6 months ago

Hi, I'm able to reproduce this issue but wasn't able to figure out yet what might be the problem. It seems to freeze when calling the native DocumentReference.Set(data) method, but I have no idea why. When I have time I will try to dig deeper, any hints or help is appreciated :)

petrdrabek commented 6 months ago

@TobiasBuchholz Hi! Any update on this? Still facing this issue in production :(

TobiasBuchholz commented 6 months ago

HI @petrdrabek, I was able to take a deeper look into the problem but unfortunately couldn't find a solution. Regarding to the firebase documentation persistence should be activated by default, which it is and you can see that at CrossFirebaseFirestore.Current.Settings.IsPersistenceEnabled. As stated before the code is getting stuck when calling the async methods of the native packages implementations, so it might be a bug in there. In addition I've noticed that calling CrossFirebaseFirestore.Current.DisableNetworkAsync() results in the same behavior, even when the device is connected to the internet.

Furthermore I've found this stackoverflow post which suggests this is intented behavior. So it seems like not awaiting the calls to firestore is the way to go...using document.AddSnapshotListener<T>() might also help to circumvent this problem.

AdamEssenmacher commented 5 months ago

Is this on iOS, Android, or both?

petrdrabek commented 5 months ago

@AdamEssenmacher I tested it only on Android.

AdamEssenmacher commented 5 months ago

Oh I know what's going on here.

The Firestore SDKs don't really do 'async'. Instead, they expect callbacks to be passed on for success/fail.

This is a pretty typical thing for native Objective-C and Java. The .NET bindings try to make this more C-sharpy by wrapping these callback patterns into typical C# async tasks.

So, @TobiasBuchholz is correct. The C# async set/updates methods will not return until saving successfully through to the server by design.

If you do not care to know when a write operation has been saved remotely, and only want to ensure it has been saved locally (and let Firestore maybe sync it up later), you can use WriteBatch.CommitLocal() which I added in #208