Cloud Firestore is a flexible, scalable database for mobile, web, and server development. It stores data in documents, which are organized into collections, and syncs data in real-time across devices.
Click to reveal answer
beginner
How do you add a document to a Firestore collection in Swift?
Use the Firestore instance to reference a collection, then call addDocument(data:) with a dictionary of key-value pairs representing your data.
Click to reveal answer
beginner
What is the difference between a document and a collection in Firestore?
A document is a single record that contains fields with data. A collection is a group of documents. Collections can contain documents, and documents can contain subcollections.
Click to reveal answer
intermediate
How does Firestore handle real-time updates?
Firestore lets your app listen to document or collection changes. When data changes, Firestore sends updates instantly to keep your UI in sync without manual refresh.
Click to reveal answer
intermediate
What is a Firestore query and how is it used?
A Firestore query lets you retrieve documents that match specific conditions, like filtering by a field value or ordering results. Queries help you get only the data you need.
Click to reveal answer
Which Firestore data structure holds multiple documents?
ADatabase
BDocument
CField
DCollection
✗ Incorrect
A collection is a container for multiple documents in Firestore.
How do you listen for real-time updates in Firestore on iOS?
ACall getDocuments() repeatedly
BUse a timer to refresh data
CUse addSnapshotListener() on a document or collection reference
DUse URLSession to fetch data
✗ Incorrect
addSnapshotListener() sets up a real-time listener that triggers when data changes.
What type of data does a Firestore document store?
AKey-value pairs
BOnly strings
COnly numbers
DOnly arrays
✗ Incorrect
Documents store data as key-value pairs, where values can be various types.
Which method adds a new document with an auto-generated ID in Firestore?
AaddDocument(data:)
BsetData(_:)
CupdateData(_:)
Ddelete()
✗ Incorrect
addDocument(data:) creates a new document with a unique ID automatically.
How can you filter documents in Firestore?
ABy using setData(_:)
BBy using whereField() in a query
CBy calling delete()
DBy using addSnapshotListener()
✗ Incorrect
whereField() lets you specify conditions to filter documents in queries.
Explain how you would set up a Firestore listener in Swift to update your app UI in real-time.
Think about how Firestore notifies your app when data changes.
You got /5 concepts.
Describe the structure of data in Cloud Firestore and how documents and collections relate.
Imagine folders and files on your computer.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of Cloud Firestore in an iOS app?
easy
A. To compile Swift code into an app
B. To design the app's user interface
C. To handle user authentication only
D. To store and sync app data in the cloud with live updates
Solution
Step 1: Understand Cloud Firestore's role
Cloud Firestore is a cloud database that stores app data and syncs it live across devices.
Step 2: Compare options with Firestore's function
Only To store and sync app data in the cloud with live updates describes storing and syncing data live, which matches Firestore's purpose.
Final Answer:
To store and sync app data in the cloud with live updates -> Option D
Quick Check:
Firestore = cloud data storage and sync [OK]
Hint: Firestore is for cloud data storage and live sync [OK]
Common Mistakes:
Confusing Firestore with UI design tools
Thinking Firestore only handles authentication
Believing Firestore compiles code
2. Which Swift code snippet correctly gets a reference to a Firestore collection named "users"?
easy
A. let usersRef = Firestore.firestore().collection("users")
B. let usersRef = Firestore.collection("users")
C. let usersRef = Firestore.getCollection("users")
D. let usersRef = Firestore.firestore().get("users")
Solution
Step 1: Recall Firestore syntax for collection reference
The correct way is to call Firestore.firestore() then .collection("name") to get a collection reference.
Step 2: Check each option's method calls
Only let usersRef = Firestore.firestore().collection("users") uses Firestore.firestore().collection("users"), which is correct syntax.
Final Answer:
let usersRef = Firestore.firestore().collection("users") -> Option A
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.