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.73k stars 3.98k forks source link

cloud_firestore: iOS includeMetadataChanges does not recieve isFromCache false after first run #12722

Closed ramsayamarin closed 6 months ago

ramsayamarin commented 6 months ago

Is there an existing issue for this?

Which plugins are affected?

Core, Database, Storage, Other

Which platforms are affected?

iOS

Description

The docs say the following about includeMetadataChanges:

Whether the snapshot was created from cached data rather than guaranteed up-to-date server data. If you called DocumentReference. snapshots or Query. snapshots with includeMetadataChanges parameter set to true you will receive another snapshot with isFromCache equal to false once the client has received up-to-date data from the backend.

On an Android device, this is true, and the snapshot is received twice, from the cache and then from server, but on iOS a cached followed by a server snapshot is only delivered the first time the query is executed, and if the same query is recreated later on the same data, a server snapshot is not received until the target data is updated. This was not the case in previous versions of the Firebase plugins or on Android, but it appeared in the latest version on iOS when I updated the plugins.

Reproducing the issue

Create a snapshot query like this example:

final query = templateQuery
          .startAt([geoHash])
          .endAt(['$geoHash~'])
          .limit(geoHashFetchCount)
          .snapshots(includeMetadataChanges: true)
          .listen(
            (snapshot) {
              if (snapshot.metadata.isFromCache) {
                return;
              }
             else...  //This part is never reached if the query gets recreated on the same data, unless the data is updated

Firebase Core version

2.30.1

Flutter Version

3.19.6

Relevant Log Output

[βœ“] Flutter (Channel stable, 3.19.6, on Microsoft Windows [Version 10.0.22631.3527], locale en-US)
[βœ“] Windows Version (Installed version of Windows is version 10 or higher)
[βœ“] Android toolchain - develop for Android devices (Android SDK version 34.0.0-rc2)
[βœ“] Chrome - develop for the web
[βœ“] Visual Studio - develop Windows apps (Visual Studio Community 2022 17.6.0)
[βœ“] Android Studio (version 2022.2)
[βœ“] IntelliJ IDEA Ultimate Edition (version 2024.1)
[βœ“] Connected device (4 available)
[βœ“] Network resources

β€’ No issues found!

Flutter dependencies

Expand Flutter dependencies snippet
```yaml cupertino_icons: ^1.0.2 firebase_core: ^2.13.1 firebase_auth: ^4.7.2 flutter_facebook_auth: ^6.0.4 provider: ^6.0.1 google_maps_flutter: ^2.5.3 http: ^1.2.1 uuid: ^4.2.2 url_launcher: ^6.0.17 google_sign_in: ^6.0.1 cloud_firestore: ^4.8.0 firebase_storage: ^11.2.2 flutter_svg: ^2.0.2 cached_network_image: ^3.2.3 flutter_image_compress: ^2.1.0 image: ^4.0.8 equatable: ^2.0.3 flutter_bloc: ^8.0.1 firebase_app_check: ^0.2.1+8 tuple: ^2.0.0 path_provider: ^2.0.9 image_picker: ^1.0.7 intl: ^0.18.0 flutter_keyboard_visibility: ^6.0.0 cloud_functions: ^4.0.3 shimmer: ^3.0.0 geolocator: ^11.0.0 location: ^6.0.1 #geoflutterfire: ^3.0.3 removed because it's not being updated to be compatible with other plugins firebase_remote_config: ^4.0.0 crypto: ^3.0.2 firebase_messaging: ^14.6.2 infinite_scroll_pagination: ^4.0.0 firebase_analytics: ^10.0.4 hive: ^2.2.2 hive_flutter: ^1.1.0 google_mobile_ads: ^5.0.0 path: ^1.8.1 sizer: ^2.0.15 collection: ^1.16.0 firebase_dynamic_links: ^5.2.0 share_plus: ^9.0.0 age_calculator: ^1.0.0 country_code: ^1.0.0 loader_overlay: ^4.0.0 device_preview: ^1.1.0 fluttertoast: ^8.0.9 rxdart: ^0.27.5 html: ^0.15.0 flutter_app_badger: ^1.5.0 permission_handler: ^11.0.1 flutter_local_notifications: ^17.0.0 vector_math: ^2.1.2 auto_size_text: ^3.0.0 flutter_native_splash: ^2.2.17 flutter_launcher_icons: ^0.13.0 in_app_review: ^2.0.6 firebase_crashlytics: ^3.0.17 firebase_app_installations: ^0.2.2 geocoding: ^3.0.0 purchases_flutter: ^6.20.0 intl_phone_number_input: ^0.7.3+1 device_region: ^1.0.1 facebook_app_events: ^0.19.0 flutter_animate: ^4.5.0 appsflyer_sdk: ^6.13.0+2 scrollable_positioned_list: ^0.3.8 sign_in_with_apple: ^6.1.0 ```

Additional context and comments

No response

TarekkMA commented 6 months ago

Hello @ramsayamarin, can you try to upgrade your firebase packages to latest version and try again?

ramsayamarin commented 6 months ago

@TarekkMA All my packages are on the latest version (4.17.2 for cloud_firestore for example), it is not shown here but in pubspec.lock they are on the latest. example:

  cloud_firestore:
    dependency: "direct main"
    description:
      name: cloud_firestore
      sha256: "..."
      url: "https://pub.dev"
    source: hosted
    version: "4.17.2"

This bug only appeared after I updated.

TarekkMA commented 6 months ago

Okay thank you @ramsayamarin, will try to reproduce this issue.

TarekkMA commented 6 months ago

@ramsayamarin I did reproduce the issue on ios (android works fine), will try to work on a fix for it.

TarekkMA commented 6 months ago

@ramsayamarin, it appears that the issue may originate from the iOS SDK. I have submitted a ticket to address this: Firebase iOS SDK Issue #12869.

ramsayamarin commented 6 months ago

@ramsayamarin, it appears that the issue may originate from the iOS SDK. I have submitted a ticket to address this: Firebase iOS SDK Issue #12869.

Thank you! I really appreciate it.

ericbomgardner commented 6 months ago

I was also hit by this issue when upgrading cloud_firestore to 4.17.0 (from 4.4.5)

Is it possible that https://github.com/firebase/flutterfire/pull/12585 could be the cause? Looks like that change was rolled out in 4.17.0 (according to the changelog) and is related to caching behavior.

ramsayamarin commented 6 months ago

hey @TarekkMA can you please share your reproduction here

TarekkMA commented 6 months ago

Hey @ramsayamarin, I will create a native ios reproduction and test and provide it.

TarekkMA commented 6 months ago

@ramsayamarin Can you please try and use latest version via git, https://dart.dev/tools/pub/dependencies#git-packages and let me know if the issue is now fixed.

ramsayamarin commented 6 months ago

@TarekkMA Sure, can you please share how exactly that would look like in a pubspec?

This is not working:

  cloud_firestore:
    git: https://github.com/firebase/flutterfire.git
TarekkMA commented 6 months ago

Can you try to use this:

  cloud_firestore:
    git:
      url: https://github.com/firebase/flutterfire.git
      path: packages/cloud_firestore/cloud_firestore
ramsayamarin commented 6 months ago

@TarekkMA Yes it seems to be working now, thank you! is it coming in 4.17.3?

TarekkMA commented 6 months ago

Yeah the next release is already out. you can try and see if it works.