17

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.

Mircea
  • 1,671
  • 7
  • 25
  • 41
  • Do you have any security rules? – Adrian Murray Jun 03 '19 at 20:25
  • Yes, I do `service cloud.firestore { match /databases/{database}/documents { match /{document=**} { // allow read, write: if false; allow read, write; } } }` – Mircea Jun 03 '19 at 20:27
  • 4
    I believe security rules can cause reads but that's not likely happening here. Do you have the Firestore Console open while you're doing this? Because seeing documents there count as reads. – Adrian Murray Jun 03 '19 at 20:30
  • Yes but I have it just on Database->Usage, and it's not really the reason. I mean I've restarted the page countless times and it didn't happen anything. I see the increasing number of reads just after I'm writing a user from app. – Mircea Jun 03 '19 at 20:33

1 Answers1

23

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.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • 7
    Thanks for the suggestion that keeping the Firebase console open will incur reads. The only interaction I had with Firebase in my code was this line: `await db.collection(collection).doc(id).set(docData);` and somehow my read count was through the roof! Once I closed the Firebase Cloud Firestore data console, the read count stopped increasing. – drigofonte Oct 15 '19 at 16:54
  • 1
    Im having the same problem understanding how reads are calculated: https://stackoverflow.com/questions/63005295/cloud-firestore-read-quota?noredirect=1#comment111417284_63005295. My console had 9 writes. My reads got to 53000 in less than an hour with a single collection, 3 documents each having 2 attributes. I have the same unexplained read count - I can't see how my setup is "a busy console". Clarity around how reads are calculated is hard to find in the cloud firestore documentation. @drigofonte - did you figure this out? – Mel Jul 21 '20 at 01:01
  • 1
    If you open the Network tab in Chrome while the Firestore console is opened, you can see the requests going in the background. These cumulate large read counts. I just visited the console with one collection one document and leave it open for 30 sec and read count was around 9-12 reads. So yes, don't ever visit console for long. – Najam Nov 24 '20 at 13:52