0
0
iOS Swiftmobile~10 mins

Cloud Firestore in iOS Swift - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the Firestore module in Swift.

iOS Swift
import [1]
Drag options to blanks, or click blank then click option'
AFirebaseFirestore
BUIKit
CFoundation
DSwiftUI
Attempts:
3 left
💡 Hint
Common Mistakes
Importing UIKit or Foundation instead of FirebaseFirestore.
Forgetting to import any Firestore-related module.
2fill in blank
medium

Complete the code to get a Firestore database instance.

iOS Swift
let db = Firestore.[1]()
Drag options to blanks, or click blank then click option'
Adefault
BfirestoreClient
Cshared
Dfirestore
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'shared' or 'default' which are not Firestore methods.
Trying to create a new instance instead of using the provided method.
3fill in blank
hard

Fix the error in the code to add data to a Firestore collection.

iOS Swift
db.collection("users").[1](data: ["name": "Alice", "age": 30]) { error in
  if let error = error {
    print("Error adding document: \(error)")
  } else {
    print("Document added successfully")
  }
}
Drag options to blanks, or click blank then click option'
AaddDocument
BsetData
CupdateDocument
DcreateDocument
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'setData' which requires a document reference, not a collection.
Using 'updateDocument' which updates existing documents only.
4fill in blank
hard

Fill both blanks to read documents from a Firestore collection and print their data.

iOS Swift
db.collection("products").[1] { querySnapshot, error in
  if let error = error {
    print("Error getting documents: \(error)")
  } else {
    for document in [2]?.documents ?? [] {
      print(document.data())
    }
  }
}
Drag options to blanks, or click blank then click option'
AgetDocuments
Bdocuments
CquerySnapshot
DaddSnapshotListener
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'addSnapshotListener' for one-time fetch.
Confusing the querySnapshot variable name.
5fill in blank
hard

Fill all three blanks to update a specific document's field in Firestore.

iOS Swift
let docRef = db.collection("orders").document([1])
docRef.[2](["status": [3]]) { error in
  if let error = error {
    print("Error updating document: \(error)")
  } else {
    print("Document successfully updated")
  }
}
Drag options to blanks, or click blank then click option'
A"order123"
BsetData
C"shipped"
DupdateData
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'setData' which overwrites the whole document.
Not quoting the document ID or string values.