You have a Firestore collection named users. You want to display the name field of each user in a RecyclerView. Which code snippet correctly listens for real-time updates and updates the UI?
val db = FirebaseFirestore.getInstance() db.collection("users") .addSnapshotListener { snapshots, e -> if (e != null) { Log.w("Firestore", "Listen failed.", e) return@addSnapshotListener } val userList = mutableListOf<String>() for (doc in snapshots!!.documents) { userList.add(doc.getString("name") ?: "Unknown") } // update RecyclerView adapter here with userList }
Remember that addSnapshotListener provides real-time updates and you must check for errors before processing data.
Option A correctly listens for real-time updates on the users collection, checks for errors, and extracts the name field from each document to update the UI. Option A only fetches data once without real-time updates. Option A tries to use map on snapshots which is a QuerySnapshot and does not have a direct map method. Option A extracts the wrong field email instead of name.
What happens if you call set() on a Firestore document that already exists without specifying SetOptions.merge()?
Think about whether set() replaces or merges data by default.
By default, set() overwrites the entire document. To merge new data with existing fields, you must use SetOptions.merge(). Options A, B, and C are incorrect behaviors.
You added a Firestore addSnapshotListener in your Activity's onCreate() method. What is the best practice to avoid memory leaks related to this listener?
Think about how to properly clean up listeners to prevent leaks.
Firestore listeners return a ListenerRegistration object. You must call remove() on it to stop listening and avoid memory leaks. Doing this in onDestroy() ensures cleanup when the Activity is destroyed. Option A is false because listeners persist unless removed. Option A may stop updates too early. Option A does not stop the listener.
Which of the following Kotlin Firestore queries correctly retrieves documents from the orders collection where the status field equals "shipped" and orders the results by date descending?
Check the order of method calls and parameter types for orderBy.
Option B uses the correct method chaining and passes the enum Query.Direction.DESCENDING to orderBy. Option B has the correct methods but the order of whereEqualTo and orderBy matters for Firestore indexes and is less recommended. Option B incorrectly passes a string instead of the enum for direction. Option B uses invalid syntax for where.
Your Android app tries to read documents from Firestore but always gets a permission denied error. Your security rules are:
service cloud.firestore {
match /databases/{database}/documents {
match /users/{userId} {
allow read: if request.auth.uid == userId;
}
}
}Your app calls db.collection("users").get() to read all users. Why does this fail?
Consider how the rules restrict access by document ID and user ID.
The security rules allow reading only documents where the document ID equals the authenticated user's ID. Trying to read the entire users collection violates this rule because it includes documents for other users. Option C might be true if unauthenticated, but the question implies authentication. Option C is false; the rules syntax is valid. Option C is incorrect; get() is valid for collections.