Hint: Use Firestore.firestore().collection("name") to get collection [OK]
Common Mistakes:
Omitting .firestore() before .collection()
Using non-existent methods like getCollection()
Calling get("users") instead of collection("users")
3. Given this Swift code, what will be printed if the document exists with field "name" = "Alice"?
let docRef = Firestore.firestore().collection("users").document("user123")
docRef.getDocument { document, error in
if let document = document, document.exists {
let name = document.data()?["name"] as? String ?? "Unknown"
print(name)
} else {
print("No document")
}
}
medium
A. Unknown
B. No document
C. Alice
D. Error
Solution
Step 1: Understand the getDocument completion handler
If the document exists, it extracts the "name" field and prints it; otherwise prints "No document".
Step 2: Given document exists with "name" = "Alice"
The code will print the value "Alice" from the document field.
Final Answer:
Alice -> Option C
Quick Check:
Document exists with name = Alice, so print Alice [OK]
Hint: If document exists, print its field value [OK]
Common Mistakes:
Assuming print shows "No document" even if document exists
Confusing default "Unknown" with actual field value
Ignoring optional cast and default fallback
4. What is wrong with this Swift Firestore code snippet?
let docRef = Firestore.firestore().collection("users").document("user123")
docRef.getDocument { document, error in
if error != nil {
print("Error fetching document")
}
if let document = document, document.exists {
print(document.data())
}
}
medium
A. It does not handle the case when document does not exist
B. It uses wrong method getDocument instead of get()
C. It should unwrap error with if let error = error
D. It tries to print document.data() without casting
Solution
Step 1: Analyze error and document handling
The code prints error if error exists, then prints data if document exists, but does nothing if document does not exist and no error.
Step 2: Identify missing handling for non-existing document
There is no else clause for when document does not exist, so no message is shown in that case.
Final Answer:
It does not handle the case when document does not exist -> Option A
Quick Check:
Missing else for document.exists = problem [OK]
Hint: Always handle document not existing case [OK]
Common Mistakes:
Confusing getDocument with get() method
Not unwrapping error properly but still works
Printing data() without casting is allowed
5. You want to listen for live updates to a Firestore collection "messages" and update your UI in SwiftUI. Which code snippet correctly sets up a listener?
hard
A. Firestore.firestore().collection("messages").getDocuments { snapshot, error in
let messages = snapshot?.documents.map { $0.data() } ?? []
self.messages = messages
}
B. Firestore.firestore().collection("messages").addSnapshotListener { snapshot, error in
guard let snapshot = snapshot else { return }
let messages = snapshot.documents.map { $0.data() }
DispatchQueue.main.async {
self.messages = messages
}
}
C. Firestore.firestore().collection("messages").document("messages").addSnapshotListener { snapshot, error in
if let snapshot = snapshot {
self.messages = snapshot.data()
}
}
D. Firestore.firestore().collection("messages").listen { snapshot, error in
self.messages = snapshot?.documents ?? []
}
Solution
Step 1: Identify method for live updates
addSnapshotListener listens for real-time updates; getDocuments fetches once.
Step 2: Check correct usage of addSnapshotListener and UI update
Firestore.firestore().collection("messages").addSnapshotListener { snapshot, error in
guard let snapshot = snapshot else { return }
let messages = snapshot.documents.map { $0.data() }
DispatchQueue.main.async {
self.messages = messages
}
} uses addSnapshotListener on collection, maps documents to data, and updates UI on main thread.
Step 3: Verify other options
Firestore.firestore().collection("messages").getDocuments { snapshot, error in
let messages = snapshot?.documents.map { $0.data() } ?? []
self.messages = messages
} fetches once, not live. Firestore.firestore().collection("messages").document("messages").addSnapshotListener { snapshot, error in
if let snapshot = snapshot {
self.messages = snapshot.data()
}
} listens on a document named "messages" (wrong). Firestore.firestore().collection("messages").listen { snapshot, error in
self.messages = snapshot?.documents ?? []
} uses non-existent listen method.
Final Answer:
Firestore.firestore().collection("messages").addSnapshotListener { snapshot, error in
guard let snapshot = snapshot else { return }
let messages = snapshot.documents.map { $0.data() }
DispatchQueue.main.async {
self.messages = messages
}
} -> Option B
Quick Check:
Use addSnapshotListener for live updates [OK]
Hint: Use addSnapshotListener for live Firestore updates [OK]
Common Mistakes:
Using getDocuments for live updates
Listening on wrong document instead of collection
Using non-existent listen method
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.