DarshanGowda0 / GeoFlutterFire

:fire:GeoFlutterFire:fire: is an open-source library that allows you to store and query firestore documents based on their geographic location.
https://youtu.be/MYHVyl-juUk
MIT License
305 stars 261 forks source link

9 Snapshot Listeners for 1 query? #161

Open 0radek opened 3 years ago

0radek commented 3 years ago

I'm using the .within() method and I notice that 9 snapshot listeners are created because of the surrounding geohashes.

image

Here's my code for what I'm doing - is this problematic at all? How to close the 9 listeners?

class SpacesStreamResults extends HookWidget {
  Stream<List<DocumentSnapshot<Map<String, dynamic>>>>? spacesStream;

  @override
  Widget build(BuildContext context) {
    final _spacesProvider = useProvider(spacesProvider);
    final _categoriesProvider = useProvider(categoriesProvider);

    useEffect(() {
      final queryRef = _spacesProvider.selectedCategories.isEmpty
          ? FirebaseFirestore.instance.collection('spaces').where(
                'open',
                isEqualTo: true,
              )
          : FirebaseFirestore.instance
              .collection('spaces')
              .where(
                'open',
                isEqualTo: true,
              )
              .where(
                'categories',
                arrayContainsAny: _spacesProvider.selectedCategories
                    .map((category) => "${category.id}")
                    .toList(),
              );

      this.spacesStream = geo
          .collection(
            collectionRef: queryRef,
          )
          .within(
              center: GeoFirePoint(36, -120),
              radius: 30,
              field: 'position');

      return () {
        this.spacesStream = null;
      };
    }, [_spacesProvider.selectedCategories]);

    return StreamBuilder<List<DocumentSnapshot<Map<String, dynamic>>>>(
      key: Key('spaces-streamBuilder'),
      stream: spacesStream,
      // initialData: [],
      builder: (BuildContext context,
          AsyncSnapshot<List<DocumentSnapshot<Map<String, dynamic>>>>
              spacesSnapshot) {

              // .................
              }
        ));
    }
}
Hu1buerger commented 3 years ago

Dosnt the GeoFireCollectionRef have a dispose() method?

cedvdb commented 2 years ago

don't quote me on this but I believe this is expected because it has to query multiple geohash squares. I don't know if 9 is exact but there is going to be more than 1 query.

That's one reason why we need Firestore itself to support Geoqueries.