1

I am using flutter with redux for state management. When the app loads I am loading the data from firestore to populate my redux state. Now the problem is every time the app initializes it makes 800+ document reads. The only source of changes to the user data is the app itself and since firestore local persistence is enabled shouldn't the data load from the local cache instead?..What am I missing?

Interesting thing is, If I disable the internet connection to the device, load the app and then enable the connection, the app still works fine (presumably without the doc reads from the app initialization process since they are coming from the cache).

Basically, the desired behavior would be the app to initializes with the local data and then sync only the updates made by the user to the firebase server.

class _MyAppState extends State<MyApp> {
    final store = new Store<AppState>(
    appReducer,
    initialState:  new AppState(isLoading: true, logs: []),
    middleware: [thunkMiddleware]);


    initState(){
        super.initState();
        loadData();
      }

   loadData(){
        FirebaseAuth.instance.currentUser().then( (loggedInUser) async {
          if(loggedInUser==null){
            store.dispatch(SetUser(null));
          }
          QuerySnapshot logsQuery = await Firestore.instance.collection("/users/"+loggedInUser.uid+"/logs").getDocuments();
          List<Log> logs = [];
          final logDocs = logsQuery.documents;
          for (DocumentSnapshot logDoc in logDocs) {
            logs.add(Log.fromFireStoreData(logDoc.documentID, logDoc.data));
          }
          store.dispatch(SetUserLogs(logs));
        }
      }
...
    }
Aasiz
  • 647
  • 8
  • 18

1 Answers1

0

I would say the problem isn't even redux or firestore persistence since there is not enough information (Does it work without redux? How are you getting that data? Show us a bit of code) in the post I'll try to answer as is but you should elaborate a bit more.

It is very important to note there is a BIG PROBLEM if you have 800+ reads from basically anything, nothing in any kind of app should be remotely able to generate 800 reads upfront.

First of all you have to optimize this user data querying and probably the firebase structure for the data, for that I recommend watching this video from the official Firebase YouTube channel and reading this article in the Firebase documentation about pricing and this one on best practices. Be very careful on how you're handling data from now on and if that does not solve your problem explain more of it on your thread.

Doodles
  • 313
  • 2
  • 12
  • Thanks for the response and the resources. I have added a code snippet to the question. To summarize: is it possible for the app to initializes with the local data and then sync only the updates made by the user to the firebase server. – Aasiz Apr 28 '19 at 09:38