Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firestore - unexpected reads

there guys, I do have an interesting problem here and I would be really glad if any of you it will be able to help me with that.

What's my app flow:

  1. Register with the email, password and some other details:
  2. User firebase in order to auth the user and create an account via email and password, at the same time I'm writing the custom data of the user to the database.
  3. Log in the user.

That's it, that's all my basic logic, and how you can see I'm not doing any reading from the DB so far as I know.

Now... the problem is that from some weird reason when I'm registering my user I'm going to the firebase console to see the usage of my DB and I will see something like... for one user which was created I will have 1 write (which is fine as I was expected) but also 13-20 READS FROM DB.

Now that's my question, WHY on earth I have reads on firestorm when I'm doing just auth and writes?

Here it's my DB code which I'm using right now.

class DatabaseFirebase implements BaseDataBase {
  final FirebaseAuth _firebaseAuth = FirebaseAuth.instance;
  final FirebaseStorage _storage = FirebaseStorage.instance;

  FirebaseUser _firebaseUser;
  Firestore _firestore = Firestore.instance;

  @override
  Future<String> login(String email, String password) async {
    _firebaseUser = await _firebaseAuth.signInWithEmailAndPassword(
        email: email, password: password);
    return _firebaseUser.uid;
  }

  @override
  Future<String> register(String email, String password) async {
    _firebaseUser = await _firebaseAuth.createUserWithEmailAndPassword(
        email: email, password: password);
    return _firebaseUser.uid;
  }

  @override
  Future<UserData> getCurrentUser() async {
    if (_firebaseUser == null)
      _firebaseUser = await _firebaseAuth.currentUser();
    UserData user = UserData();
    user.email = _firebaseUser?.email;
    user.name = _firebaseUser?.displayName;
    return user;
  }

  @override
  Future<void> logout() async {
    _firebaseAuth.signOut();
  }

  @override
  Future<void> onAuthStateChanged(void Function(FirebaseUser) callback) async {
    _firebaseAuth.onAuthStateChanged.listen(callback);
  }

  @override
  Future<void> writeUser(UserData user) async {
    _firestore.collection("Users").add(user.toMap()).catchError((error) {
      print(error);
    });
  }
}

If some of you know could you explain to me where/how I need to search in order to find this bug? Because how you can see I'm not using any read what so ever.

like image 574
Mircea Avatar asked Jun 03 '19 20:06

Mircea


People also ask

Why are my reads so high firestore?

If you leave the console open on a collection or document with busy write activity then the Firebase console will automatically read the changes that update the console's display. Most of the time this is the reason for unexpected high reads.

Why do I have so many reads from Firebase?

use of the Firebase console will incur reads. If you leave the console open on a collection/document with busy write activity, the console will automatically read the changes that update the console's display. This is very often the source of unexpected reads.

How many reads can firestore handle?

10 for single-document requests and query requests. 20 for multi-document reads, transactions, and batched writes. The previous limit of 10 also applies to each operation.

What counts as a read on Firestore?

Listening to query results Cloud Firestore allows you to listen to the results of a query and get realtime updates when the query results change. When you listen to the results of a query, you are charged for a read each time a document in the result set is added or updated.


1 Answers

It's impossible to know for sure given that we don't understand all possible routes of access into your database, but you should be aware that use of the Firebase console will incur reads. If you leave the console open on a collection/document with busy write activity, the console will automatically read the changes that update the console's display. This is very often the source of unexpected reads.

Without full reproduction steps of exactly all the steps you're taking, there's no way to know for sure.

Firebase currently does not provide tools to track the origin of document reads. If you need to measure specific reads from your app, you will have to track that yourself somehow.

like image 93
Doug Stevenson Avatar answered Oct 15 '22 12:10

Doug Stevenson