Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flutter Firestore take long retrieving data while offline #31393

Closed
ShadyBoshra2012 opened this issue Apr 21, 2019 · 10 comments
Closed

Flutter Firestore take long retrieving data while offline #31393

ShadyBoshra2012 opened this issue Apr 21, 2019 · 10 comments
Labels
p: cloud_firestore Firebase Firestore plugin package flutter/packages repository. See also p: labels.

Comments

@ShadyBoshra2012
Copy link

Steps to Reproduce

I am using Firestore in flutter application. Each time user launch the application it retrieves some data from cloud.

QuerySnapshot dataSnapshot = await Firestore.instance
        .collection('/data')
        .getDocuments();

When user opens the application on first time, it required from him to connect online, to get the data, and as Firebase documents say

For Android and iOS, offline persistence is enabled by default. To disable persistence, set the PersistenceEnabled option to false.

So, it should save the data that application have been read before to retrieve it while the device is offline; so user can access application at anytime with the same data that have been read.

The problem is: it takes long time to retrieve the data while the device is offline, with the same codes and nothing changed!.

I tried to configure how much time it takes? On offline, it takes about 8 minutes and 40 seconds. But while on online, it takes just 10 seconds, maybe less.

Logs

------------------------------------------------------------
Gradle 4.10.2
------------------------------------------------------------

Build time:   2018-09-19 18:10:15 UTC
Revision:     b4d8d5d170bb4ba516e88d7fe5647e2323d791dd

Kotlin DSL:   1.0-rc-6
Kotlin:       1.2.61
Groovy:       2.4.15
Ant:          Apache Ant(TM) version 1.9.11 compiled on March 23 2018
JVM:          1.8.0_152-release (JetBrains s.r.o 25.152-b01)
OS:           Windows 10 10.0 amd64
[√] Flutter (Channel stable, v1.2.1, on Microsoft Windows [Version 10.0.17134.648], locale en-US)
    • Flutter version 1.2.1 at E:\flutter
    • Framework revision 8661d8aecd (9 weeks ago), 2019-02-14 19:19:53 -0800
    • Engine revision 3757390fa4
    • Dart version 2.1.2 (build 2.1.2-dev.0.0 0a7dcf17eb)

[√] Android toolchain - develop for Android devices (Android SDK version 28.0.3)
    • Android SDK at C:\Users\shady\AppData\Local\Android\sdk
    • Android NDK location not configured (optional; useful for native profiling support)
    • Platform android-28, build-tools 28.0.3
    • ANDROID_HOME = C:\Users\shady\AppData\Local\Android\sdk
    • Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java
    • Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1343-b01)
    • All Android licenses accepted.

[√] Android Studio (version 3.4)
    • Android Studio at C:\Program Files\Android\Android Studio
    • Flutter plugin version 34.0.2
    • Dart plugin version 183.5901
    • Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1343-b01)

[√] VS Code (version 1.32.3)
    • VS Code at C:\Users\shady\AppData\Local\Programs\Microsoft VS Code
    • Flutter extension version 2.24.0

[√] Connected device (1 available)
    • Android SDK built for x86 • emulator-5554 • android-x86 • Android 8.1.0 (API 27) (emulator)

• No issues found!
@edsheehy
Copy link

I am having the same problem.
Just wondering if you found a solution?

@ShadyBoshra2012
Copy link
Author

Unfortunately not, waiting to be checked from Firebase Flutter developers

@edsheehy
Copy link

I noticed that if you wait for a few minutes and then try it again, the retrieve time goes back down to milliseconds.

@ride4sun
Copy link

ride4sun commented Sep 6, 2019

@edsheedy any news on that. Please vote the original question up that this get attention.

@diegoveloper
Copy link
Member

Same issue here

@ra9r
Copy link

ra9r commented Sep 23, 2019

This is a critical performance issue for us. Offline access should be lightning fast and one of the major reasons for selecting Firestore over other solutions.

@diegoveloper
Copy link
Member

I found a workaround for this issue, before calling Firestore, I get if there is an active internet connection using connectivity package from Google : https://pub.dev/packages/connectivity .

If there is no connection we can change the source from Firestore : https://pub.dev/documentation/cloud_firestore/latest/cloud_firestore/Source-class.html

 final Source source =
        hasActiveConnection ? Source.serverAndCache : Source.cache;

And we can set the source to get the documents or document.

await Firestore.instance
          .collection("Collection")
          .getDocuments(source: source)

@ShadyBoshra2012
Copy link
Author

And finally, with the help of @diegoveloper comment, I have reached the solution.

This comment

await Firestore.instance .collection("Collection") .getDocuments(source: source)

was a good solution if I decided to check source each time and then use it or I can use it in starting of a new Flutter project, but now I already have a lot of codes that need a better solution. So I decided to fork the cloud_firestore package and edit it.

You can find it here: https://github.com/ShadyBoshra2012/flutterfire/tree/master/packages/cloud_firestore

What I have edited:

  1. firestore.dart
// The source of which the data will come from.
 static Source _source = Source.serverAndCache;

 static Source get source => _source;

 Future<void> settings(
     {bool persistenceEnabled,
     String host,
     bool sslEnabled,
     bool timestampsInSnapshotsEnabled,
     int cacheSizeBytes,
     Source source}) async {
   await channel.invokeMethod<void>('Firestore#settings', <String, dynamic>{
     'app': app.name,
     'persistenceEnabled': persistenceEnabled,
     'host': host,
     'sslEnabled': sslEnabled,
     'timestampsInSnapshotsEnabled': timestampsInSnapshotsEnabled,
     'cacheSizeBytes': cacheSizeBytes,
   });
   if (source != null) _source = source;
 }
  1. query.dart
    source = Firestore.source; Line 92

  2. document_reference.dart
    source = Firestore.source; Line 83

How you can use it?

So you can use my forked repository in this way with using connectivity package from Google : https://pub.dev/packages/connectivity .

Add my forked repository in pubspec.yaml file

cloud_firestore:
    git:
      url: https://github.com/ShadyBoshra2012/flutterfire.git
      path: packages/cloud_firestore

Then in your first screen or main

var connectivityResult = await (Connectivity().checkConnectivity());
if (connectivityResult == ConnectivityResult.none) {
    await Firestore.instance.settings(source: Source.cache);
} else {
    await Firestore.instance.settings(source: Source.serverAndCache);
}

and if you want to refresh the source when change the connection state:

StreamSubscription subscription;

void initState() {
    super.initState();
    // Check the internet connection after each change
    // of the connection.
    subscription = Connectivity()
        .onConnectivityChanged
        .listen((ConnectivityResult result) async {
      // Check the internet connection and then choose the appropriate
      // source for it.
      var connectivityResult = await (Connectivity().checkConnectivity());
      if (connectivityResult == ConnectivityResult.none) {
        await Firestore.instance.settings(source: Source.cache);
      } else {
        await Firestore.instance.settings(source: Source.serverAndCache);
      }
    });
}

@override
  void dispose() {
    super.dispose();
    subscription.cancel();
  }

So I hope it works with everyone see it, and waiting for Flutter Team to code a better and better solution. Thanks for everyone has participated.

@kroikie
Copy link

kroikie commented Oct 13, 2019

@ShadyBoshra2012

This issue has been moved to firebase/flutterfire#1231. Any further collaboration will be done there.

@github-actions
Copy link

This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 27, 2021
@flutter-triage-bot flutter-triage-bot bot added the package flutter/packages repository. See also p: labels. label Jul 5, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
p: cloud_firestore Firebase Firestore plugin package flutter/packages repository. See also p: labels.
Projects
None yet
Development

No branches or pull requests

7 participants