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.71k stars 3.97k forks source link

πŸ› [FIRESTORE] Query is too slow #9476

Closed itssidhere closed 1 year ago

itssidhere commented 2 years ago

Bug report

Describe the bug A clear and concise description of what the bug is.

Steps to reproduce

Steps to reproduce the behavior:

  1. Go to '...'
  2. '...'
  3. See error or incorrect behavior

Expected behavior

A clear and concise description of what you expected to happen.

Sample project

   return collectionReference
        .where('timestamp', isGreaterThan: oneHourAgoTime)
        .orderBy('timestamp', descending: true)
        .limit(10)
        .snapshots()
        .transform(StreamTransformer<QuerySnapshot<Map<String, dynamic>>,
                List<RoomChat>>.fromHandlers(
            handleData: (QuerySnapshot<Map<String, dynamic>> querySnapshot,
                    EventSink<List<RoomChat>> sink) =>
                mapQuerySnapshotToRoomChat(querySnapshot, sink)));

Additional context

I am experiencing an issue. When the user has subscribed to this query for a long and has retrieved multiple documents. This query becomes slow over time as reported in my production app. The user has to clear their app's cache. Can you please tell me how should I proceed with this? I want this query to fetch documents from the server and not the local cache in order to avoid this issue but still want persistence enabled in my app. I looked up on the internet and found a method to create a local index for a java client.

setIndexConfiguration


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, 3.3.0, on Microsoft Windows [Version 10.0.22000.856], locale en-US) [!] Android toolchain - develop for Android devices (Android SDK version 32.1.0-rc1) X cmdline-tools component is missing Run `path/to/sdkmanager --install "cmdline-tools;latest"` See https://developer.android.com/studio/command-line for more details. X Android license status unknown. Run `flutter doctor --android-licenses` to accept the SDK licenses. See https://flutter.dev/docs/get-started/install/windows#android-setup for more details. [√] Chrome - develop for the web [!] Visual Studio - develop for Windows (Visual Studio Community 2019 16.11.17) X Visual Studio is missing necessary components. Please re-run the Visual Studio installer for the "Desktop development with C++" workload, and include these components: MSVC v142 - VS 2019 C++ x64/x86 build tools - If there are multiple build tool versions available, install the latest C++ CMake tools for Windows Windows 10 SDK [√] Android Studio (version 2021.2) [√] VS Code (version 1.71.0) [√] Connected device (3 available) [√] HTTP Host Availability ! Doctor found issues in 2 categories.

Flutter dependencies

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

Click To Expand Dart SDK 2.18.0 Flutter SDK 3.3.0 mohalla 0.1.4+1044 dependencies: - android_id 0.0.7 [flutter] - animations 2.0.4 [flutter] - audio_session 0.1.10 [flutter flutter_web_plugins rxdart meta] - audioplayers 0.20.1 [uuid path_provider http flutter flutter_web_plugins] - auto_size_text 3.0.0 [flutter] - awesome_dialog 2.2.1 [flutter simple_animations flare_flutter] - badges 2.0.3 [flutter] - better_player 0.0.82 [flutter cupertino_icons wakelock meta flutter_widget_from_html_core visibility_detector path_provider collection xml] - bubble 1.2.1 [flutter] - cloud_firestore 3.4.6 [cloud_firestore_platform_interface cloud_firestore_web collection firebase_core firebase_core_platform_interface flutter meta] - cloud_functions 3.3.6 [cloud_functions_platform_interface cloud_functions_web firebase_core firebase_core_platform_interface flutter] - collection 1.16.0 - equatable 2.0.5 [collection meta] - extended_image 6.2.1 [extended_image_library flutter meta] - firebase_analytics 9.3.3 [firebase_analytics_platform_interface firebase_analytics_web firebase_core firebase_core_platform_interface flutter] - firebase_auth 3.7.0 [firebase_auth_platform_interface firebase_auth_web firebase_core firebase_core_platform_interface flutter meta] - firebase_core 1.21.1 [firebase_core_platform_interface firebase_core_web flutter meta] - firebase_crashlytics 2.8.9 [firebase_core firebase_core_platform_interface firebase_crashlytics_platform_interface flutter stack_trace] - firebase_database 9.1.3 [firebase_core firebase_core_platform_interface firebase_database_platform_interface firebase_database_web flutter] - firebase_dynamic_links 4.3.6 [firebase_core firebase_core_platform_interface firebase_dynamic_links_platform_interface flutter meta plugin_platform_interface] - firebase_in_app_messaging 0.6.0+23 [firebase_core firebase_core_platform_interface firebase_in_app_messaging_platform_interface flutter meta] - firebase_messaging 11.4.4 [firebase_core firebase_core_platform_interface firebase_messaging_platform_interface firebase_messaging_web flutter meta] - firebase_storage 10.3.7 [firebase_core firebase_core_platform_interface firebase_storage_platform_interface firebase_storage_web flutter] - flex_color_scheme 5.1.0 [flutter material_color_utilities] - flutter 0.0.0 [characters collection material_color_utilities meta vector_math sky_engine] - flutter_bloc 7.3.3 [flutter bloc provider] - flutter_colorpicker 1.0.3 [flutter] - flutter_datetime_picker 1.5.1 [flutter] - flutter_facebook_auth 4.4.1 [flutter flutter_facebook_auth_platform_interface flutter_facebook_auth_web] - flutter_linkify 5.0.2 [flutter linkify] - flutter_local_notifications 9.1.4 [clock flutter flutter_local_notifications_linux flutter_local_notifications_platform_interface timezone] - flutter_rating_bar 4.0.1 [flutter] - flutter_screenutil 5.5.3+2 [flutter] - flutter_slidable 1.3.2 [flutter] - flutter_sound 9.2.13 [path_provider recase uuid provider path synchronized logger flutter flutter_sound_platform_interface flutter_sound_web] - flutter_spinkit 5.1.0 [flutter] - flutter_staggered_grid_view 0.6.2 [flutter] - flutter_typeahead 3.2.7 [flutter flutter_keyboard_visibility] - flutter_vibrate 1.3.0 [flutter] - fluttertoast 8.0.9 [flutter flutter_web_plugins] - font_awesome_flutter 10.1.0 [flutter] - get 4.6.5 [flutter] - get_it 7.2.0 [async collection] - giphy_get 3.1.0 [flutter flutter_localizations provider extended_image flutter_staggered_grid_view meta http url_launcher] - google_fonts 3.0.1 [flutter http path_provider crypto] - google_sign_in 5.4.1 [flutter google_sign_in_android google_sign_in_ios google_sign_in_platform_interface google_sign_in_web] - hive 2.2.3 [meta crypto] - hive_flutter 1.1.0 [flutter hive path_provider path] - image_cropper 2.0.3 [flutter image_cropper_platform_interface image_cropper_for_web] - image_gallery_saver 1.7.1 [flutter] - image_picker 0.8.5+3 [flutter image_picker_android image_picker_for_web image_picker_ios image_picker_platform_interface] - implicitly_animated_reorderable_list 0.4.2 [flutter async meta] - in_app_purchase 3.0.7 [flutter in_app_purchase_android in_app_purchase_platform_interface in_app_purchase_storekit] - in_app_update 3.0.0 [flutter] - infinite_scroll_pagination 3.2.0 [flutter sliver_tools] - intl 0.17.0 [clock path] - json_annotation 4.6.0 [meta] - like_button 2.0.4 [flutter] - local_auth 2.1.2 [flutter intl local_auth_android local_auth_ios local_auth_platform_interface local_auth_windows] - lottie 1.4.2 [archive flutter path vector_math] - page_transition 2.0.9 [flutter] - paginate_firestore 1.0.3 [flutter bloc flutter_bloc cloud_firestore provider] - palette_generator 0.3.3+2 [collection flutter path] - path_provider 2.0.11 [flutter path_provider_android path_provider_ios path_provider_linux path_provider_macos path_provider_platform_interface path_provider_windows] - permission_handler 10.0.0 [flutter meta permission_handler_android permission_handler_apple permission_handler_windows permission_handler_platform_interface] - photo_view 0.13.0 [flutter] - profanity_filter 2.0.0 - provider 6.0.3 [collection flutter nested] - random_color 1.0.6-nullsafety [flutter] - rate_my_app 1.1.3 [shared_preferences flutter_rating_bar flutter] - readmore 2.2.0 [flutter] - screenshot 1.2.3 [flutter] - select_dialog 2.0.0 [flutter] - share_plus 4.0.10+1 [meta mime flutter share_plus_platform_interface share_plus_linux share_plus_macos share_plus_windows share_plus_web] - shimmer 2.0.0 [flutter] - slider_button 1.0.0 [vibration flutter] - smooth_page_indicator 1.0.0+2 [flutter] - swipe_to 1.0.2 [flutter] - timeago 3.3.0 [intl] - transparent_image 2.0.0 - tutorial_coach_mark 1.2.4 [flutter] - url_launcher 6.1.5 [flutter url_launcher_android url_launcher_ios url_launcher_linux url_launcher_macos url_launcher_platform_interface url_launcher_web url_launcher_windows] - uuid 3.0.6 [crypto] - visibility_detector 0.2.2 [flutter] - wakelock 0.6.2 [flutter meta wakelock_macos wakelock_platform_interface wakelock_web wakelock_windows] - youtube_explode_dart 1.11.0 [collection freezed_annotation html http http_parser json_annotation meta xml] dev dependencies: - build_runner 2.2.0 [args async analyzer build build_config build_daemon build_resolvers build_runner_core code_builder collection crypto dart_style frontend_server_client glob graphs http_multi_server io js logging meta mime package_config path pool pub_semver pubspec_parse shelf shelf_web_socket stack_trace stream_transform timing watcher web_socket_channel yaml] - hive_generator 1.1.3 [build source_gen hive analyzer source_helper] - json_serializable 6.3.1 [analyzer async build build_config collection json_annotation meta path pub_semver pubspec_parse source_gen source_helper] - pedantic 1.11.1 transitive dependencies: - _fe_analyzer_shared 46.0.0 [meta] - analyzer 4.6.0 [_fe_analyzer_shared collection convert crypto glob meta package_config path pub_semver source_span watcher yaml] - archive 3.3.1 [crypto path] - args 2.3.1 - async 2.9.0 [collection meta] - bloc 7.2.1 [meta] - boolean_selector 2.1.0 [source_span string_scanner] - build 2.3.0 [analyzer async convert crypto glob logging meta path] - build_config 1.1.0 [checked_yaml json_annotation path pubspec_parse yaml] - build_daemon 3.1.0 [built_collection built_value http_multi_server logging path pool shelf shelf_web_socket stream_transform watcher web_socket_channel] - build_resolvers 2.0.9 [analyzer async build crypto graphs logging path package_config pool pub_semver stream_transform yaml] - build_runner_core 7.2.3 [async build build_config build_resolvers collection convert crypto glob graphs json_annotation logging meta path package_config pool timing watcher yaml] - built_collection 5.1.1 - built_value 8.4.1 [built_collection collection fixnum meta] - characters 1.2.1 - checked_yaml 2.0.1 [json_annotation source_span yaml] - clock 1.1.1 - cloud_firestore_platform_interface 5.7.3 [collection firebase_core flutter meta plugin_platform_interface] - cloud_firestore_web 2.8.6 [cloud_firestore_platform_interface collection firebase_core firebase_core_web flutter flutter_web_plugins js] - cloud_functions_platform_interface 5.1.15 [firebase_core flutter meta plugin_platform_interface] - cloud_functions_web 4.3.4 [cloud_functions_platform_interface firebase_core firebase_core_web flutter flutter_web_plugins js] - code_builder 4.2.0 [built_collection built_value collection matcher meta] - convert 3.0.2 [typed_data] - cross_file 0.3.3+1 [js meta] - crypto 3.0.2 [typed_data] - csslib 0.17.2 [source_span] - cupertino_icons 1.0.5 - dart_style 2.2.3 [analyzer args path pub_semver source_span] - dbus 0.5.4 [args meta pedantic xml] - extended_image_library 3.3.0 [crypto flutter http_client_helper path path_provider] - fake_async 1.3.1 [clock collection] - ffi 2.0.1 - file 6.1.4 [meta path] - firebase_analytics_platform_interface 3.3.3 [firebase_core flutter meta plugin_platform_interface] - firebase_analytics_web 0.4.2+3 [firebase_analytics_platform_interface firebase_core firebase_core_web flutter flutter_web_plugins js] - firebase_auth_platform_interface 6.6.0 [collection firebase_core flutter meta plugin_platform_interface] - firebase_auth_web 4.3.0 [firebase_auth_platform_interface firebase_core firebase_core_web flutter flutter_web_plugins http_parser intl js meta] - firebase_core_platform_interface 4.5.1 [collection flutter flutter_test meta plugin_platform_interface] - firebase_core_web 1.7.2 [firebase_core_platform_interface flutter flutter_web_plugins js meta] - firebase_crashlytics_platform_interface 3.2.15 [collection firebase_core flutter meta plugin_platform_interface] - firebase_database_platform_interface 0.2.2+3 [collection firebase_core flutter meta plugin_platform_interface] - firebase_database_web 0.2.1+5 [firebase_core firebase_core_web firebase_database_platform_interface flutter flutter_web_plugins js] - firebase_dynamic_links_platform_interface 0.2.3+11 [firebase_core flutter meta plugin_platform_interface] - firebase_in_app_messaging_platform_interface 0.2.1+15 [firebase_core flutter meta plugin_platform_interface] - firebase_messaging_platform_interface 3.5.4 [firebase_core flutter meta plugin_platform_interface] - firebase_messaging_web 2.4.4 [firebase_core firebase_core_web firebase_messaging_platform_interface flutter flutter_web_plugins js meta] - firebase_storage_platform_interface 4.1.15 [collection firebase_core flutter meta plugin_platform_interface] - firebase_storage_web 3.3.5 [async firebase_core firebase_core_web firebase_storage_platform_interface flutter flutter_web_plugins http js meta] - fixnum 1.0.1 - flare_flutter 3.0.2 [collection flutter meta] - flutter_facebook_auth_platform_interface 3.2.0 [flutter plugin_platform_interface] - flutter_facebook_auth_web 3.2.0 [flutter flutter_web_plugins js flutter_facebook_auth_platform_interface] - flutter_keyboard_visibility 5.3.0 [meta flutter_keyboard_visibility_platform_interface flutter_keyboard_visibility_web flutter] - flutter_keyboard_visibility_platform_interface 2.0.0 [flutter meta plugin_platform_interface] - flutter_keyboard_visibility_web 2.0.0 [flutter_keyboard_visibility_platform_interface flutter_web_plugins flutter] - flutter_local_notifications_linux 0.3.0 [flutter flutter_local_notifications_platform_interface dbus path xdg_directories] - flutter_local_notifications_platform_interface 5.0.0 [flutter plugin_platform_interface] - flutter_localizations 0.0.0 [flutter intl characters clock collection material_color_utilities meta path vector_math] - flutter_plugin_android_lifecycle 2.0.7 [flutter] - flutter_sound_platform_interface 9.2.13 [flutter meta plugin_platform_interface logger] - flutter_sound_web 9.2.13 [flutter_sound_platform_interface flutter logger flutter_web_plugins meta js] - flutter_test 0.0.0 [flutter test_api path fake_async clock stack_trace vector_math async boolean_selector characters collection matcher material_color_utilities meta source_span stream_channel string_scanner term_glyph] - flutter_web_plugins 0.0.0 [flutter js characters collection material_color_utilities meta vector_math] - flutter_widget_from_html_core 0.8.5+3 [csslib flutter fwfh_text_style html] - freezed_annotation 1.1.0 [collection json_annotation meta] - frontend_server_client 2.1.3 [async path] - fwfh_text_style 2.22.08+1 [flutter] - glob 2.1.0 [async collection file path string_scanner] - google_sign_in_android 6.1.0 [flutter google_sign_in_platform_interface] - google_sign_in_ios 5.5.0 [flutter google_sign_in_platform_interface] - google_sign_in_platform_interface 2.3.0 [flutter plugin_platform_interface quiver] - google_sign_in_web 0.10.2 [flutter flutter_web_plugins google_sign_in_platform_interface js] - graphs 2.1.0 [collection] - html 0.15.0 [csslib source_span] - http 0.13.5 [async http_parser meta path] - http_client_helper 2.0.3 [http] - http_multi_server 3.2.1 [async] - http_parser 4.0.1 [collection source_span string_scanner typed_data] - image_cropper_for_web 0.0.4 [flutter flutter_web_plugins image_cropper_platform_interface js] - image_cropper_platform_interface 2.0.0 [flutter plugin_platform_interface http] - image_picker_android 0.8.5+2 [flutter flutter_plugin_android_lifecycle image_picker_platform_interface] - image_picker_for_web 2.1.8 [flutter flutter_web_plugins image_picker_platform_interface] - image_picker_ios 0.8.6 [flutter image_picker_platform_interface] - image_picker_platform_interface 2.6.1 [cross_file flutter http plugin_platform_interface] - in_app_purchase_android 0.2.3+3 [collection flutter in_app_purchase_platform_interface json_annotation] - in_app_purchase_platform_interface 1.3.1 [flutter plugin_platform_interface] - in_app_purchase_storekit 0.3.2 [collection flutter in_app_purchase_platform_interface json_annotation] - io 1.0.3 [meta path string_scanner] - js 0.6.4 - linkify 4.1.0 - local_auth_android 1.0.11 [flutter flutter_plugin_android_lifecycle intl local_auth_platform_interface] - local_auth_ios 1.0.9 [flutter intl local_auth_platform_interface] - local_auth_platform_interface 1.0.4 [flutter intl plugin_platform_interface] - local_auth_windows 1.0.3 [flutter local_auth_platform_interface] - logger 1.1.0 - logging 1.0.2 - matcher 0.12.12 [stack_trace] - material_color_utilities 0.1.5 - meta 1.8.0 - mime 1.0.2 - nested 1.0.0 [flutter] - package_config 2.1.0 [path] - path 1.8.2 - path_provider_android 2.0.20 [flutter path_provider_platform_interface] - path_provider_ios 2.0.11 [flutter path_provider_platform_interface] - path_provider_linux 2.1.7 [ffi flutter path path_provider_platform_interface xdg_directories] - path_provider_macos 2.0.6 [flutter path_provider_platform_interface] - path_provider_platform_interface 2.0.4 [flutter platform plugin_platform_interface] - path_provider_windows 2.1.2 [ffi flutter path path_provider_platform_interface win32] - permission_handler_android 10.0.0 [flutter permission_handler_platform_interface] - permission_handler_apple 9.0.4 [flutter permission_handler_platform_interface] - permission_handler_platform_interface 3.7.0 [flutter meta plugin_platform_interface] - permission_handler_windows 0.1.0 [flutter permission_handler_platform_interface] - petitparser 5.0.0 [meta] - platform 3.1.0 - plugin_platform_interface 2.1.2 [meta] - pool 1.5.1 [async stack_trace] - process 4.2.4 [file path platform] - pub_semver 2.1.1 [collection meta] - pubspec_parse 1.2.1 [checked_yaml collection json_annotation pub_semver yaml] - quiver 3.1.0 [matcher] - recase 4.0.0 - rxdart 0.27.5 - share_plus_linux 3.0.0 [share_plus_platform_interface file flutter meta url_launcher] - share_plus_macos 3.0.1 [share_plus_platform_interface flutter] - share_plus_platform_interface 3.0.3 [flutter meta mime plugin_platform_interface] - share_plus_web 3.0.1 [share_plus_platform_interface url_launcher flutter flutter_web_plugins meta] - share_plus_windows 3.0.1 [share_plus_platform_interface flutter meta url_launcher] - shared_preferences 2.0.15 [flutter shared_preferences_android shared_preferences_ios shared_preferences_linux shared_preferences_macos shared_preferences_platform_interface shared_preferences_web shared_preferences_windows] - shared_preferences_android 2.0.12 [flutter shared_preferences_platform_interface] - shared_preferences_ios 2.1.1 [flutter shared_preferences_platform_interface] - shared_preferences_linux 2.1.1 [file flutter path path_provider_linux path_provider_platform_interface shared_preferences_platform_interface] - shared_preferences_macos 2.0.4 [flutter shared_preferences_platform_interface] - shared_preferences_platform_interface 2.1.0 [flutter plugin_platform_interface] - shared_preferences_web 2.0.4 [flutter flutter_web_plugins shared_preferences_platform_interface] - shared_preferences_windows 2.1.1 [file flutter path path_provider_platform_interface path_provider_windows shared_preferences_platform_interface] - shelf 1.3.2 [async collection http_parser path stack_trace stream_channel] - shelf_web_socket 1.0.2 [shelf stream_channel web_socket_channel] - simple_animations 4.2.0 [flutter collection] - sky_engine 0.0.99 - sliver_tools 0.2.7 [flutter] - source_gen 1.2.2 [analyzer async build dart_style glob meta path source_span yaml] - source_helper 1.3.2 [analyzer collection source_gen] - source_span 1.9.0 [collection path term_glyph] - stack_trace 1.10.0 [path] - stream_channel 2.1.0 [async] - stream_transform 2.0.0 - string_scanner 1.1.1 [source_span] - synchronized 3.0.0+2 - term_glyph 1.2.1 - test_api 0.4.12 [async boolean_selector collection meta source_span stack_trace stream_channel string_scanner term_glyph matcher] - timezone 0.8.0 [path] - timing 1.0.0 [json_annotation] - typed_data 1.3.1 [collection] - url_launcher_android 6.0.17 [flutter url_launcher_platform_interface] - url_launcher_ios 6.0.17 [flutter url_launcher_platform_interface] - url_launcher_linux 3.0.1 [flutter url_launcher_platform_interface] - url_launcher_macos 3.0.1 [flutter url_launcher_platform_interface] - url_launcher_platform_interface 2.1.0 [flutter plugin_platform_interface] - url_launcher_web 2.0.13 [flutter flutter_web_plugins url_launcher_platform_interface] - url_launcher_windows 3.0.1 [flutter url_launcher_platform_interface] - vector_math 2.1.2 - vibration 1.7.6 [flutter] - wakelock_macos 0.4.0 [flutter flutter_web_plugins wakelock_platform_interface] - wakelock_platform_interface 0.3.0 [flutter meta] - wakelock_web 0.4.0 [flutter flutter_web_plugins js wakelock_platform_interface] - wakelock_windows 0.2.0 [flutter wakelock_platform_interface win32] - watcher 1.0.1 [async path] - web_socket_channel 2.2.0 [async crypto stream_channel] - win32 2.7.0 [ffi] - xdg_directories 0.2.0+2 [meta path process] - xml 5.4.1 [collection meta petitparser] - yaml 3.1.1 [collection source_span string_scanner]

darshankawar commented 2 years ago

@itssidhere Thanks for the report. Can you provide any logs that shows duration indicating slowness while retrieving the data ? Is it possible for you to debug at native side and see if slowness actually happens while executing the query ? Can you also provide sample document you're querying against and also let us know about the amount of documents in the DB ?

itssidhere commented 2 years ago

@darshankawar If I try to use clearPersistance everything gets back to normal. I think it's mainly because I usually clear the room chats from cloud function and the client side sdk isn't aware of this so it tries to persist every document. The query should be indexed locally just like in Java sdk using setIndexConfig or there should be a parameter in .snapshots() just like in .get() to specify the source of the document cache or server. Please implement setIndexConfig.

darshankawar commented 2 years ago

@itssidhere Thanks for the feedback. Can you provide an example that shows the actual / current result (ie, slow execution of query) and usage of clearPersistence that helps resolving the issue along with how setIndexConfiguration would help to resolve slow / performance of the query execution ?

itssidhere commented 2 years ago

@darshankawar Any complex query like this

collectionReference
        .where('timestamp', isGreaterThan: oneHourAgoTime)
        .orderBy('timestamp', descending: true)
        .limit(10)
        .snapshots()

Suppose you start listening to the query and try to add multiple documents (enough to fill up the local cache size). Then simply try to re-attach the listener on the same query you will notice a long delay as this query is not getting locally indexed as it gets on the server side. Clearing the persistence can speed it up again as there is no local cache. Android has an API setIndexConfiguration which sets the index for the local cache( if I am not wrong) to speed up the query locally.

This is my actual usage -

Bloc

 Stream<RoomChatState> mapFetchRoomChatEventToState() async* {
    try {
      yield InitialRoomChatState();

      await streamSubscription?.cancel();

      streamSubscription = roomChatRepo!.getRoomChat().listen((data) {
        roomChats = data;
        add(ReceivedRoomChatEvent(roomChat: data)); //it then yields RoomChatReceivedState

      });
    } catch (exception) {
      print(exception);
    }
  }

RoomChatRepo

Stream<List<RoomChat>> getRoomChat() {
    CollectionReference collectionReference =
        db.collection(PathOf.mohallaChats).doc(mohalla).collection(room);
    final now = Timestamp.now().toDate();
    final oneHourAgoTime =
        Timestamp.fromDate(now.subtract(const Duration(hours: 1)));

    return collectionReference
        .where('timestamp', isGreaterThan: oneHourAgoTime)
        .orderBy('timestamp', descending: true)
        .limit(10)
        .snapshots()
        .transform(StreamTransformer<QuerySnapshot<Map<String, dynamic>>,
                List<RoomChat>>.fromHandlers(
            handleData: (QuerySnapshot<Map<String, dynamic>> querySnapshot,
                    EventSink<List<RoomChat>> sink) =>
                mapQuerySnapshotToRoomChat(querySnapshot, sink)));
  }

RoomChatPage

BlocBuilder<RoomChatBloc, RoomChatState>(
                                  bloc: roomChatBloc,
                                  builder: (context, state) {
                                    if (state is RoomChatReceivedState) {
                                      if (showCachedList.value) {
                                        return cachedChatList;
                                      }
                                      cachedChatList =
                                          ImplicitlyAnimatedList<RoomChat>(
                                        reverse: true,
                                        controller: _scrollController,
                                        areItemsTheSame:
                                            (RoomChat a, RoomChat b) =>
                                                a.id == b.id,
                                        items: state.roomChat,
                                        itemBuilder: (context,
                                            Animation<double> animation,
                                            item,
                                            index) {
                                          return FadeTransition(
                                            key: ValueKey(item.id),
                                            opacity: animation,
                                            child: RoomChat()
                                      );

                                      return cachedChatList;
                                    } else {
                                      return RoomChatLoading();
                                    }
                                  },
                                )

To fix the delay I have to call clearPersistance in my main()

Future<void> main() async {
 WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);
 await FirebaseFirestore.instance.clearPersistence();

}

As you can see the bloc starts from InitialRoomChatState. After fetching 100s of documents if you re-attach the listeners by calling mapFetchRoomChatEventToState it gets stuck to InitialRoomChatState for a significant amount of time.

darshankawar commented 2 years ago

Thanks for this update. Wondering if you get same behavior without using Bloc ? I am trying to see if the query execution speed behavior is happening without any external dependencies.

itssidhere commented 2 years ago

@darshankawar That is hard to test as we need to populate the cache enough to start noticing the difference. Since it's a production app that is doable but producing a seperate example without any external dependency is hard. Though i am sure about it as clearPersistance fixes the issue so it has something to do with the firestore.

darshankawar commented 2 years ago

I am going ahead and label it for now for team's insights based on above comment. But if you could, a reproducible code sample without third party plugins would be nice to actually narrow down this behavior to the plugin.

/cc @russellwheatley

larssn commented 2 years ago

@itssidhere Are you using Source.cache anywhere? I've noticed that being very slow lately.

itssidhere commented 2 years ago

@larssn No but the plugin implicitly tries to get it from cache first so that's why it's the same thing. Cache is very slow and disabling it is the only option.

Peirazo commented 2 years ago

I am also experiencing this same issue too from android. Here is my experience...

When the user install the app, everything is normal and the queries are very fast for both Source.cache and Source.server. Then after using the app for a while, all queries becomes extremely slow for both Source.cache and Source.server and the more the user keeps using the app, the slower it becomes. The problem persist even after the user manually clears the cache.

The queries only becomes faster again after the user uninstall the app then reinstall it. But as they keep using the app, the queries becomes slower and slower with time.

Things I have tried:

None of this has helped, I only have to uninstall the app and reinstall for the queries to be fast again, but becomes slower and slower with time.

itssidhere commented 2 years ago

@Peirazo True. @darshankawar Can you please look into this issue.

itssidhere commented 2 years ago

This issue is also discussed in the recent firebase video. But Android has a solution for this. Please implement the same in flutter.

VIDEO

itssidhere commented 1 year ago

Finally it's fixed in #9928