firebase / flutterfire

🔥 A collection of Firebase plugins for Flutter apps.
https://firebase.google.com/docs/flutter/setup
BSD 3-Clause "New" or "Revised" License
8.46k stars 3.91k forks source link

🐛 [cloud_firestore] Streambuilder very slow to update from Cloud Firestore when Android app resumes after long period of inactivity #4305

Closed ratherNotB closed 2 years ago

ratherNotB commented 3 years ago

Bug report

I have observed instances where a Streambuilder takes 60-90 seconds to rebuild the UI with current Cloud Firestore data. This can occur when an Android phone running a Flutter app is locked and left inactive for a period of time... 30 minutes for example. When the phone is unlocked and the Flutter app resumed, any data written to Cloud Firestore while the phone was locked can take 60-90 seconds to appear in the app when being read via a Streambuilder.

I have tried to reproduce this issue on iOS but have been unsuccessful.

Some other noteworthy points:

I have a chat app written in Flutter that uses Cloud Firestore. It's very confusing to users when they tap on a new message notification and the app opens but they don't see the new message in their chat screen for > 60 seconds.

Steps to reproduce

Please use the sample code attached below to reproduce the issue.

  1. Create a Firebase project and create the following document in Cloud Firestore:

/demo/demoData/

  1. In the above document, initialize a "count" field to 0.
  2. Run the sample app on an Android phone.
  3. Lock the Android phone and wait 30 minutes.
  4. Increment the "count" field either via the Firebase console or using another phone running the sample app.
  5. Unlock the Android phone and wait for the "count" value to update from Cloud Firestore. It can take more than 60 seconds.

Expected behavior

I would expect the data from Cloud Firestore to update within the time it takes to perform a cold start... which I believe is ~10 seconds for Cloud Firestore.

Additional context

Sample app to reproduce the issue. Tapping the FAB increments the "count" field in Cloud Firestore.

main.dart

import 'package:flutter/material.dart';

import 'package:firebase_core/firebase_core.dart';
import 'package:cloud_firestore/cloud_firestore.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();

  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  Stream<DocumentSnapshot> _docSnapStream;

  @override
  void initState() {
    super.initState();

    _docSnapStream = FirebaseFirestore.instance.collection('demo').doc('demoData').snapshots();
  }

  void _incrementCounter() {
    DocumentReference docRef = FirebaseFirestore.instance.collection('demo').doc('demoData');
    docRef.update({'count': FieldValue.increment(1)});
  }

  @override
  Widget build(BuildContext context) {

    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'You have pushed the button this many times:',
            ),
            StreamBuilder(
              stream: _docSnapStream,
              builder: (context, snapshot) {
                if (snapshot.hasError) {
                  return Text(
                    'Snapshot contained an error',
                    style: Theme.of(context).textTheme.headline4,
                  );
                }
                switch (snapshot.connectionState) {
                  case ConnectionState.none:
                    continue waiting;
                  waiting:
                  case ConnectionState.waiting:
                    return CircularProgressIndicator(
                      strokeWidth: 5.0,
                    );

                  case ConnectionState.active:
                    continue data_ready;
                  data_ready:
                  case ConnectionState.done:
                    return buildCount(snapshot);
                }
                return null;
              },
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }

  Widget buildCount(AsyncSnapshot snapshot) {
    if (snapshot.hasError) {
      return Text(
        'Snapshot contained an error',
        style: Theme.of(context).textTheme.headline4,
      );
    } else if (snapshot == null || !snapshot.hasData || snapshot.data == null) {
      return CircularProgressIndicator(
        strokeWidth: 5.0,
      );
    } else {
      return Text(
        '${snapshot.data['count']}',
        style: Theme.of(context).textTheme.headline4,
      );
    }
  }
}

Flutter doctor

Run flutter doctor and paste the output below:

Click To Expand ``` Doctor summary (to see all details, run flutter doctor -v): [✓] Flutter (Channel stable, 1.22.3, on Mac OS X 10.15.7 19H15, locale en-CA) [✓] Android toolchain - develop for Android devices (Android SDK version 30.0.2) [✓] Xcode - develop for iOS and macOS (Xcode 12.2) [!] Android Studio (version 4.1) ✗ Flutter plugin not installed; this adds Flutter specific functionality. ✗ Dart plugin not installed; this adds Dart specific functionality. [✓] VS Code (version 1.51.0) [✓] Connected device (1 available) ! Doctor found issues in 1 category. ```

Flutter dependencies

Run flutter pub deps -- --style=compact and paste the output below:

Click To Expand ``` Dart SDK 2.10.3 Flutter SDK 1.22.3 baby_names 1.0.0+1 dependencies: - cloud_firestore 0.14.3 [flutter meta quiver firebase_core firebase_core_platform_interface cloud_firestore_platform_interface cloud_firestore_web] - cupertino_icons 1.0.0 - firebase_core 0.5.2 [firebase_core_platform_interface flutter quiver meta firebase_core_web] - flutter 0.0.0 [characters collection meta typed_data vector_math sky_engine] dev dependencies: - flutter_test 0.0.0 [flutter test_api path fake_async clock stack_trace vector_math async boolean_selector characters charcode collection matcher meta source_span stream_channel string_scanner term_glyph typed_data] transitive dependencies: - async 2.5.0-nullsafety.1 [collection] - boolean_selector 2.1.0-nullsafety.1 [source_span string_scanner] - characters 1.1.0-nullsafety.3 - charcode 1.2.0-nullsafety.1 - clock 1.1.0-nullsafety.1 - cloud_firestore_platform_interface 2.2.0 [flutter meta collection firebase_core plugin_platform_interface] - cloud_firestore_web 0.2.1 [flutter flutter_web_plugins http_parser meta firebase_core firebase_core_web cloud_firestore_platform_interface js] - collection 1.15.0-nullsafety.3 - fake_async 1.2.0-nullsafety.1 [clock collection] - firebase_core_platform_interface 2.0.0 [flutter meta plugin_platform_interface quiver] - firebase_core_web 0.2.1 [firebase_core_platform_interface flutter flutter_web_plugins meta js] - flutter_web_plugins 0.0.0 [flutter characters collection meta typed_data vector_math] - http_parser 3.1.4 [charcode collection source_span string_scanner typed_data] - js 0.6.2 - matcher 0.12.10-nullsafety.1 [stack_trace] - meta 1.3.0-nullsafety.3 - path 1.8.0-nullsafety.1 - plugin_platform_interface 1.0.3 [meta] - quiver 2.1.5 [matcher meta] - sky_engine 0.0.99 - source_span 1.8.0-nullsafety.2 [charcode collection path term_glyph] - stack_trace 1.10.0-nullsafety.1 [path] - stream_channel 2.1.0-nullsafety.1 [async] - string_scanner 1.1.0-nullsafety.1 [charcode source_span] - term_glyph 1.2.0-nullsafety.1 - test_api 0.2.19-nullsafety.2 [async boolean_selector collection meta path source_span stack_trace stream_channel string_scanner term_glyph matcher] - typed_data 1.3.0-nullsafety.3 [collection] - vector_math 2.1.0-nullsafety.3 ```

markusaksli-nc commented 3 years ago

I didn't see any delay after waiting for 30 minutes on the latest master 1.25.0-5.0.pre.45.

flutter doctor -v ``` [√] Flutter (Channel master, 1.25.0-5.0.pre.45, on Microsoft Windows [Version 10.0.19041.630], locale et-EE) • Flutter version 1.25.0-5.0.pre.45 at C:\Development\flutter_master • Framework revision 81e1f7d1ed (5 hours ago), 2020-12-02 17:19:57 -0800 • Engine revision 20caf54969 • Dart version 2.12.0 (build 2.12.0-76.0.dev) [√] Android toolchain - develop for Android devices (Android SDK version 30.0.2) • Android SDK at C:\Users\marku\AppData\Local\Android\sdk • Platform android-30, build-tools 30.0.2 • Java binary at: C:\Users\marku\AppData\Local\JetBrains\Toolbox\apps\AndroidStudio\ch-0\201.6953283\jre\bin\java • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b01) • All Android licenses accepted. [√] Chrome - develop for the web • Chrome at C:\Program Files\Google\Chrome\Application\chrome.exe [√] Visual Studio - develop for Windows (Visual Studio Community 2019 16.7.7) • Visual Studio at C:\Program Files (x86)\Microsoft Visual Studio\2019\Community • Visual Studio Community 2019 version 16.7.30621.155 • Windows 10 SDK version 10.0.18362.0 [√] Android Studio (version 4.1.0) • Android Studio at C:\Users\marku\AppData\Local\JetBrains\Toolbox\apps\AndroidStudio\ch-0\201.6953283 • Flutter plugin can be installed from: https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: https://plugins.jetbrains.com/plugin/6351-dart • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b01) [√] Connected device (4 available) • Windows (desktop) • windows • windows-x64 • Microsoft Windows [Version 10.0.19041.630] • Web Server (web) • web-server • web-javascript • Flutter Tools • Chrome (web) • chrome • web-javascript • Google Chrome 87.0.4280.88 • Edge (web) • edge • web-javascript • Microsoft Edge 87.0.664.47 • No issues found! ```
ratherNotB commented 3 years ago

I forgot to mention that the issue doesn't occur if the phone is connected to my computer via ADB while waiting the 30 minutes.

Also, the issue doesn't occur every time I wait 30 minutes. I would say it happens ~50% of the time.

Thanks

On Thu., Dec. 3, 2020, 5:26 a.m. Markus Aksli, notifications@github.com wrote:

I didn't see any delay after waiting for 30 minutes on the latest master 1.25.0-5.0.pre.45. flutter doctor -v

[√] Flutter (Channel master, 1.25.0-5.0.pre.45, on Microsoft Windows [Version 10.0.19041.630], locale et-EE)

• Flutter version 1.25.0-5.0.pre.45 at C:\Development\flutter_master

• Framework revision 81e1f7d1ed (5 hours ago), 2020-12-02 17:19:57 -0800

• Engine revision 20caf54969

• Dart version 2.12.0 (build 2.12.0-76.0.dev)

[√] Android toolchain - develop for Android devices (Android SDK version 30.0.2)

• Android SDK at C:\Users\marku\AppData\Local\Android\sdk

• Platform android-30, build-tools 30.0.2

• Java binary at: C:\Users\marku\AppData\Local\JetBrains\Toolbox\apps\AndroidStudio\ch-0\201.6953283\jre\bin\java

• Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b01)

• All Android licenses accepted.

[√] Chrome - develop for the web

• Chrome at C:\Program Files\Google\Chrome\Application\chrome.exe

[√] Visual Studio - develop for Windows (Visual Studio Community 2019 16.7.7)

• Visual Studio at C:\Program Files (x86)\Microsoft Visual Studio\2019\Community

• Visual Studio Community 2019 version 16.7.30621.155

• Windows 10 SDK version 10.0.18362.0

[√] Android Studio (version 4.1.0)

• Android Studio at C:\Users\marku\AppData\Local\JetBrains\Toolbox\apps\AndroidStudio\ch-0\201.6953283

• Flutter plugin can be installed from:

   https://plugins.jetbrains.com/plugin/9212-flutter

• Dart plugin can be installed from:

   https://plugins.jetbrains.com/plugin/6351-dart

• Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b01)

[√] Connected device (4 available)

• Windows (desktop) • windows    • windows-x64    • Microsoft Windows [Version 10.0.19041.630]

• Web Server (web)  • web-server • web-javascript • Flutter Tools

• Chrome (web)      • chrome     • web-javascript • Google Chrome 87.0.4280.88

• Edge (web)        • edge       • web-javascript • Microsoft Edge 87.0.664.47

• No issues found!

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/FirebaseExtended/flutterfire/issues/4305#issuecomment-737992793, or unsubscribe https://github.com/notifications/unsubscribe-auth/AJ662AAGMU6PSETTJHLNPNTSS6GZFANCNFSM4ULRZ2MA .

ratherNotB commented 3 years ago

A couple more points to note:

On Thu., Dec. 3, 2020, 8:51 a.m. Jim Cicalo, jim.cicalo@gmail.com wrote:

I forgot to mention that the issue doesn't occur if the phone is connected to my computer via ADB while waiting the 30 minutes.

Also, the issue doesn't occur every time I wait 30 minutes. I would say it happens ~50% of the time.

Thanks

On Thu., Dec. 3, 2020, 5:26 a.m. Markus Aksli, notifications@github.com wrote:

I didn't see any delay after waiting for 30 minutes on the latest master 1.25.0-5.0.pre.45. flutter doctor -v

[√] Flutter (Channel master, 1.25.0-5.0.pre.45, on Microsoft Windows [Version 10.0.19041.630], locale et-EE)

• Flutter version 1.25.0-5.0.pre.45 at C:\Development\flutter_master

• Framework revision 81e1f7d1ed (5 hours ago), 2020-12-02 17:19:57 -0800

• Engine revision 20caf54969

• Dart version 2.12.0 (build 2.12.0-76.0.dev)

[√] Android toolchain - develop for Android devices (Android SDK version 30.0.2)

• Android SDK at C:\Users\marku\AppData\Local\Android\sdk

• Platform android-30, build-tools 30.0.2

• Java binary at: C:\Users\marku\AppData\Local\JetBrains\Toolbox\apps\AndroidStudio\ch-0\201.6953283\jre\bin\java

• Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b01)

• All Android licenses accepted.

[√] Chrome - develop for the web

• Chrome at C:\Program Files\Google\Chrome\Application\chrome.exe

[√] Visual Studio - develop for Windows (Visual Studio Community 2019 16.7.7)

• Visual Studio at C:\Program Files (x86)\Microsoft Visual Studio\2019\Community

• Visual Studio Community 2019 version 16.7.30621.155

• Windows 10 SDK version 10.0.18362.0

[√] Android Studio (version 4.1.0)

• Android Studio at C:\Users\marku\AppData\Local\JetBrains\Toolbox\apps\AndroidStudio\ch-0\201.6953283

• Flutter plugin can be installed from:

   https://plugins.jetbrains.com/plugin/9212-flutter

• Dart plugin can be installed from:

   https://plugins.jetbrains.com/plugin/6351-dart

• Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b01)

[√] Connected device (4 available)

• Windows (desktop) • windows    • windows-x64    • Microsoft Windows [Version 10.0.19041.630]

• Web Server (web)  • web-server • web-javascript • Flutter Tools

• Chrome (web)      • chrome     • web-javascript • Google Chrome 87.0.4280.88

• Edge (web)        • edge       • web-javascript • Microsoft Edge 87.0.664.47

• No issues found!

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/FirebaseExtended/flutterfire/issues/4305#issuecomment-737992793, or unsubscribe https://github.com/notifications/unsubscribe-auth/AJ662AAGMU6PSETTJHLNPNTSS6GZFANCNFSM4ULRZ2MA .

ArafatAbsi commented 3 years ago

same issue observed here!

sebastien-mcrae commented 3 years ago

Same exact problem with a Galaxy S10 (Android 10). No problem with iOS or an Android Virtual Device.

ratherNotB commented 3 years ago

Hi @markusaksli-nc any luck reproducing the issue when your phone is not connected to your computer via ADB? This is key to reproducing the issue, as the problem won't occur when your device is connected via ADB.

This bug is a serious issue for me as many users of our app are complaining about "missing" chat messages due to this issue.... and as a result they often decide to delete our app as they view it to be unreliable.

Your help is appreciated here. Thanks

jorgegil96 commented 3 years ago

I'm seeing the exact same issue here but in the native android sdk. Exactly the same behavior, happens after app is backgrounded 1-2 hours, reconnects after 1 minute then works fine. Switching from wifi to LTE triggers an update immediately.

Has anyone been able to find a solution? Thanks

ratherNotB commented 3 years ago

Hi @jorgegil96 unfortunately I haven't found a solution and this issue doesn't seem to be getting any attention from the flutterfire team. I've tried the latest versions of the flutterfire packages and the issue still remains:

firebase_core: 0.7.0 cloud_firestore: 0.16.0 firebase_storage: 7.0.0 cloud_functions: 0.9.0 firebase_auth: 0.20.0+1 firebase_messaging: 6.0.16 firebase_analytics: 7.0.1 firebase_crashlytics: 0.4.0+1

It looks like once the "Needs Attention" label is removed from an issue there is no further follow-up.

At this point the only path to resolving this issue that I can see is to file a new bug report. Have you tried submitting a bug report for the android SDK?

https://github.com/firebase/firebase-android-sdk/issues

markusaksli-nc commented 3 years ago

I was able to reproduce this again with a release build without ADB on the latest master 1.26.0-13.0.pre.179 with cloud_firestore: ^0.16.0.

flutter doctor -v ```console [√] Flutter (Channel master, 1.26.0-13.0.pre.179, on Microsoft Windows [Version 10.0.19041.746], locale et-EE) • Flutter version 1.26.0-13.0.pre.179 at C:\Development\flutter_master • Framework revision fc9addb88b (7 hours ago), 2021-01-26 03:24:03 -0500 • Engine revision f47ab4434d • Dart version 2.12.0 (build 2.12.0-257.0.dev) [√] Android toolchain - develop for Android devices (Android SDK version 30.0.2) • Android SDK at C:\Users\marku\AppData\Local\Android\sdk • Platform android-30, build-tools 30.0.2 • Java binary at: C:\Users\marku\AppData\Local\JetBrains\Toolbox\apps\AndroidStudio\ch-0\201.7042882\jre\bin\java • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b01) • All Android licenses accepted. [√] Chrome - develop for the web • Chrome at C:\Program Files\Google\Chrome\Application\chrome.exe [√] Visual Studio - develop for Windows (Visual Studio Community 2019 16.7.7) • Visual Studio at C:\Program Files (x86)\Microsoft Visual Studio\2019\Community • Visual Studio Community 2019 version 16.7.30621.155 • Windows 10 SDK version 10.0.18362.0 [√] Android Studio (version 4.1.0) • Android Studio at C:\Users\marku\AppData\Local\JetBrains\Toolbox\apps\AndroidStudio\ch-0\201.7042882 • Flutter plugin can be installed from: https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: https://plugins.jetbrains.com/plugin/6351-dart • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b01) [√] Connected device (4 available) • SM G950F (mobile) • ce12171c51cc001c03 • android-arm64 • Android 9 (API 28) • Windows (desktop) • windows • windows-x64 • Microsoft Windows [Version 10.0.19041.746] • Chrome (web) • chrome • web-javascript • Google Chrome 87.0.4280.141 • Edge (web) • edge • web-javascript • Microsoft Edge 87.0.664.75 • No issues found! ```

You can somewhat force this behavior with adb shell dumpsys deviceidle force-idle

@ratherNotB FYI the Needs Attention label gets replaced with a plugin label when the issue is ready for the team to take a look at.

ratherNotB commented 3 years ago

Thank you @markusaksli-nc for investigating this issue further. Very excited to hear that you were able to reproduce it!

Koorenay commented 3 years ago

Exactly the same issue for me on a pixel 3a and Android 11 😤

markusaksli-nc commented 3 years ago

Please thumbs up the original issue comment if you have the same issue, that way we can track how many are facing the problem.

ratherNotB commented 3 years ago

Regarding how long this issue has existed, I've found a SO question from Aug 2020 (Flutter 1.20.2) that I believe describes this same issue:

https://stackoverflow.com/questions/63434654/my-flutter-app-with-firestore-experiences-very-slow-queries-when-it-is-resumed-f

QuentinSc commented 3 years ago

Hello, Height months with firebase and height months with this issue on my galaxy s10. It's a blocker for a deployment in production

QuentinSc commented 3 years ago

Hello,

Nobody with a workaround ?

aqwert commented 3 years ago

I too am seeing this with a release version of Android on my Galaxy Note 10. When the app is resumed from the background after a long time (could be many hours later) then the first query will take 60-90 seconds to complete.

This is a blocker since the nature of the app will have people coming in after some time of inactivity then want to save/query data in the moment. Waiting this long will be a massive fail in experience and distrust in the app.

frankyvij commented 3 years ago

Has anybody found a way around this? This is resulting in presentation of incorrect data to my users. Please @markusaksli-nc, do we have this on the priority list since you were already able to reproduce the issue.

ratherNotB commented 3 years ago

Unfortunately I haven't found a workaround and am still waiting for a fix for this highly disruptive bug.

Hi @markusaksli-nc, @kroikie; can one of you please advise as to when we can have a rough timeline for a fix? Can we expect a fix in the next 2-3 months for example? It's been almost 5 months since I filed this bug report.

If a decision has been made to not fix this bug then please let us know asap so we can plan accordingly.

Thanks

KubaStachecki commented 3 years ago

Same problem here: it takes good 20 - 50 seconds for firestore streams to 'catch up' with the data on resume. I'm considering restarting the whole app every time it gets active, but this workaround is far from optimal due to launch time and additional charges for new data downloads. Looking forward to better ideas or fix to this problem.

frankyvij commented 3 years ago

Individually you can restart the app and get the latest data, but how do you explain that to the user? This is not a good user experience, in fact, it really makes the application look really average.

@markusaksli-nc , @kroikie : Can you point me to the code that may be causing the problem.. If you don't have time, at least direct the community to fix the issue for you please 👍

Ehesp commented 3 years ago

I think this is an underlying SDK issue, which also is confirmed with this comment:

I'm seeing the exact same issue here but in the native android sdk

FlutterFire is just wrapping the native Firebase SDKs - when a query event is sent from native we relay it to Dart land via a stream. If something is taking a long time to fire an event, then either the Android SDK has an issue or Flutter isn't letting Android know about the reconnection.

frankyvij commented 3 years ago

Thanks @Ehesp , so may I please ask if this ticket itself can be referred by the Firebase SDK member, or would you internally refer this ticket to the team? Because this ticket could result in many 1 star reviews for the Flutter app.

Ehesp commented 3 years ago

I'll raise this with the sdk team, could do with a reproduction on the android level though for a proper issue to be raised.

frankyvij commented 3 years ago

Thanks @Ehesp , please let us know if there is any way that we can keep track of any progress made by the SDK team on this issue (maybe there is a respective ticket?). If the issue isn't a priority, I would highly request the SDK team to help us find an alternative (restarting app is not a solution).

frankyvij commented 3 years ago

I'm seeing the exact same issue here but in the native android sdk. Exactly the same behavior, happens after app is backgrounded 1-2 hours, reconnects after 1 minute then works fine. Switching from wifi to LTE triggers an update immediately.

Has anyone been able to find a solution? Thanks

@Ehesp , please also note that @jorgegil96 was able to reproduce this on native SDK.

Ehesp commented 3 years ago

Yeah I saw that, however actually having a project to run would be super helpful.

frankyvij commented 3 years ago

Unfortunately, I am not a native developer so I might not be able to submit a small reproducible project. @jorgegil96 could you please help us on this please?

On the other note, can you please verify this issue on Firebase Android SDK repo: https://github.com/firebase/firebase-android-sdk/issues/1258.

It seems like that a similar problem was reported last year (although they could only restore connection after 18 minutes, we can recover connection within 30-60 seconds). Issue suggests a following workaround:

Workaround: Call goOffline() followed by goOnline().

Is there a way to call these methods from flutter firestore/firebase wrapper? I want to try it out.

jorgegil96 commented 3 years ago

It took a bit but I found a way to reliably reproduce this with a sample small project (native SDK, not flutter).

You'll find a small project and the logs + repro steps in this drive folder. https://drive.google.com/drive/folders/1bWPX4IKmQqs86-Khf0CqwwzKOLlqfOEe?usp=sharing

Copying repro steps here:

I wrote a sample app that shows live data from firestore. The data itself is just a timestamp
with the current time (US Central) that's written to firestore by a python script every minute. 
So if you look at the app and the time displayed doesn't match the current time, then we know 
there's an unreasonable delay.

Repro steps:
1. Disable LTE data, just keep Wifi connected
2. Kill app, open app (around 16:23:35)
3. Wait for app to receive update from network (~16:24:03)
4. Navigate home, open google chrome to put the app in background
5. Force idle (adb shell dumpsys deviceidle force-idle)
6. Keep looking at logcat to see updates every minute (~16:25:02, 16:26:02, 16:27:02, 16:28:02)
7. Wait until there's a connection error that kicks off exponential backoff (~16:28:15 we see a connection error)
8. Open app after 8 minutes (~16:36:10) 
- Note: I was just looking at the exp backoff delay retries (it was as high as 84000s, and as soon as I saw 59947 ms I opened the app)
9. Wait with the app open until we finally get a network update at (~16:37:48)
- Note: this is about 1 minute after opening up the app, which matches the last backoff timer :O
10. Revert force-idle (adb shell dumpsys deviceidle unforce)

Expected behavior:
As soon as you re-open app (step 8) it should check the network for new data.

Current behavior:
It is not until after 60ish seconds after re-opening app that we get network data.

Repro attemps: 5/5

See the txt file in drive for more info and some useful logs from Firestore and the sample app

I feel that being backgrounded should not count as a connection error that triggers exponential backoff strategy. Or at least, the sdk should detect that we've transitioned from the bg to the foreground and retry immeditaly instead of waiting for the backoff timer to expire. It's pretty obvious that this strategy is the issue, I saw backoffs as high as 84000ms, which coincide with reports of delays anywhere from 30-90 seconds..

Workaround: Call goOffline() followed by goOnline().

I think I tried that a while ago and did not work. Only thing that works is killing and restarting the app. That workaround is from an issue in the real-time database, not firestore, so I guess it's not the same underlying issue.

The only real workaround I can think of is to migrate back to real-time db :(

frankyvij commented 3 years ago

Thanks a ton, @jorgegil96 .

@Ehesp , I hope this should be sufficient to raise an issue with SDK team and perhaps, prioritize? Performance is a key element of any application, and I believe, looking at a number of people experiencing this issue, I suggest this should be no less than a P2.

Ehesp commented 3 years ago

Thanks very much for the details and repro. I'll flag this up, however keep in mind I am not a Googler so how it's dealt with and the speed/priority is not something I can comment on.

Ehesp commented 3 years ago

Closing this as we can now track over on https://github.com/firebase/firebase-android-sdk/issues/2637

frankyvij commented 3 years ago

Hello @Ehesp , could we please release a new version of cloud_firestore and relevant libraries with the latest changes made for https://github.com/firebase/firebase-android-sdk/issues/2637. Thank you.

Ehesp commented 3 years ago

@frankyvij we need it to become available as a Firebase release first - we've got an internal ticket to keep an eye on that and create a new release when we can.

frankyvij commented 3 years ago

Thanks @Ehesp , I can only request to prioritize the release. Hope we will see a release in the next couple of days.

Ehesp commented 3 years ago

A Firebase release is outside of our control I'm afraid. Once it lands we'll make sure it's a priority on FlutterFire.

frankyvij commented 2 years ago

Hi @Ehesp , it seems that the fix has been released in firebase android SDK. Could we please plan the release for cloud_firestore with the latest SDK. Please! Thank you!

park1120 commented 2 years ago

Hi, I am also the one who waiting for this problem to be fixed. I hope to see an update to new firebase BoM soon. Thanks for all of your effort

frankyvij commented 2 years ago

I have created a fork and upgraded the firebase_core to use the latest SDK: 28.1.0. Those who just want to test in their dev environments can use this in their pubspec.yaml.

firebase_core: git: url: https://github.com/frankyvij/flutterfire/ path: packages/firebase_core/firebase_core/ ref: 9d1b4a852b060c0c1954d16f409100bc8233762b

dgilperez commented 2 years ago

@frankyvij does it work for you? Did you experienced any improvement?

frankyvij commented 2 years ago

I have been using my version of firebase core for about a day now and not even once I had to restart my app. I have never been so happy before. HAHA.

tw-tjd commented 2 years ago

This was published in cloud_firestore: ^2.2.2

QuentinSc commented 2 years ago

I'm using 2.2.2 and the issue is not resolved for me

jorgegil96 commented 2 years ago

Same here (with the native SDK) - I just posted about it in firebase/firebase-android-sdk#2637

mpiparo commented 2 years ago

Same here with 2.2.2 - which I'd expect if it's not resolved in the native SDK.

But I'm also observing that if the first firestore call after the connection timeout and the app brought back to foreground is a 'get()' on a doc that doesn't exist, for example I have a call as follows:

(await noteMatchDocRef.get()).exists;

it indefinitely hangs and never returns. not sure if anyone else has noticed that?

In any case, we really need to have these firestore db streams re-established after they shutdown when the app returns and in-use again - it doesn't seem to be an issue with my realtime db streams, they look ok after the same delay in app use.

KubaStachecki commented 2 years ago

In cloud_firestore: ^2.3.0 problem still persists.

Ehesp commented 2 years ago

The PR with the new fix has only recently been merged into Android SDK repo, we're still waiting on a Firebase release.