skip to Main Content

I have developed an app where I get a list of users constantly updated as they enter the vicinity. It shows up fast & fine when there are a few users (roughly less than 100), but takes time when it crosses the mark. Is there a particular way that I can get the list as soon as possible? It has a bunch of criteria where the list gets showed up, to which I am assuming it is taking that much time to load. Please let me know what I can do here to optimise the time and get the list ASAP. Thank you!

Conditions of getting the "make friends" list is as follows:

  1. The list of all the users who are not in friends with the current user
  2. Both the users (current user and the other user who are not friends with each other) should be on the same venue they have booked the entry for
  3. The entry is marked as "1" from the venue side once they enter the venue
  4. (assuming the user can go to multiple venues on the same day) They should get the list of users, where the last entry is marked as 1

2

Answers


  1. To increase the performance, the best way is to put all data related to the query inside the same collection, in this case, the user collection.

    Also when you are not dealing with duplicated entries, prefer using maps, instead of arrays, because it’s better for Firebase Security Rules and performing queries, so it would be something like:

    user document example:

    {
        name: "John"
        friends: { 
           "<user-id-abc...>": true,
           "<user-id-def...>": true, 
           // undefined properties will be considered as "not friend"
        },
        venues: {
           lastVenue: "<venue-id-abc...>",
           "<venue-id-abc...>": true,
           "<venue-id-def...>": true,
        }
    }
    

    So I think the query would be something similar to this:

    db.collection("users")
        .where(`friends.${friendId}`, "!=", true) // not friends
        .where("venues.lastVenue", "==", lastVenueId) // same last venue
        .get();
    
    Login or Signup to reply.
  2. How are u doing?

    In Flutter, you can use the shared_preferences package to store and retrieve small amounts of data persistently.
    You can also use packages for saving data in cache memory including photos!

    Here’s a step-by-step guide on how to get data and save it using the shared_preferences package:

    1. Add Dependency:

      Start by adding the shared_preferences package to your Flutter project. You can do this by adding the following dependency to your pubspec.yaml file:

      dependencies:
        shared_preferences: ^2.0.6 # Use the latest version
      

      After adding the dependency, run flutter pub get to fetch it.

    2. Import the Package:

      In your Dart code, import the shared_preferences package like this:

      import 'package:shared_preferences/shared_preferences.dart';
      
    3. Save Data:

      To save data using shared_preferences, you need to create an instance of SharedPreferences and then use it to store your data. Here’s an example of how to save a simple key-value pair:

      Future<void> saveData() async {
        SharedPreferences prefs = await SharedPreferences.getInstance();
        prefs.setString('key', 'value'); // Replace 'key' and 'value' with your data
      }
      

      You can use setBool, setInt, setDouble, or other similar methods depending on the type of data you want to store.

    4. Retrieve Data:

      To retrieve the saved data, you can use the following code:

      Future<void> fetchData() async {
        SharedPreferences prefs = await SharedPreferences.getInstance();
        String? data = prefs.getString('key'); // Replace 'key' with your data key
        // Check if data exists before using it
        if (data != null) {
          // Use the retrieved data
          print(data);
        }
      }
      

      Again, use getBool, getInt, getDouble, or corresponding methods if you stored different data types.

    5. Error Handling:

      Remember to handle potential exceptions when working with shared_preferences. For example, if there’s an issue with initializing or writing to storage, you should handle these exceptions gracefully.

    6. Usage:

      Call the saveData() function to save your data when needed, and call fetchData() to retrieve it. You can use these functions in response to user interactions or at specific points in your app’s lifecycle.

    Hope this helps, good luck!!

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search