Challenge - 5 Problems
Cloud Storage Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate2:00remaining
Uploading a File to Cloud Storage
What will happen when this Swift code runs to upload a file to Firebase Storage?
iOS Swift
let storage = Storage.storage() let storageRef = storage.reference() let fileURL = URL(string: "https://example.com/image.png")! let imagesRef = storageRef.child("images/image.png") imagesRef.putFile(from: fileURL, metadata: nil) { metadata, error in if let error = error { print("Upload failed: \(error.localizedDescription)") } else { print("Upload successful") } }
Attempts:
2 left
💡 Hint
Check how the URL is created and how the completion handler works.
✗ Incorrect
The code safely unwraps the URL with ! and uploads the file to the specified path. The completion handler prints success or error messages accordingly.
🧠 Conceptual
intermediate1:30remaining
Understanding Cloud Storage Security Rules
Which statement correctly describes Firebase Storage security rules behavior?
Attempts:
2 left
💡 Hint
Think about how access is controlled in cloud storage.
✗ Incorrect
Firebase Storage security rules define who can read or write files based on user authentication and file paths. They do not encrypt files or only apply to Firestore.
❓ lifecycle
advanced2:00remaining
Handling Download Task Lifecycle
What is the correct way to pause and resume a download task from Firebase Storage in Swift?
iOS Swift
let storageRef = Storage.storage().reference().child("files/video.mp4") var downloadTask: StorageDownloadTask? downloadTask = storageRef.write(toFile: localURL) { url, error in if let error = error { print("Download failed: \(error.localizedDescription)") } else { print("Download succeeded") } } // Later in code // How to pause and resume the download task?
Attempts:
2 left
💡 Hint
Check the StorageDownloadTask API for controlling downloads.
✗ Incorrect
Firebase StorageDownloadTask supports pause() and resume() methods to control the download lifecycle.
🔧 Debug
advanced2:00remaining
Diagnosing a Failed Upload Error
Given this code snippet, what is the most likely cause of the upload failure?
iOS Swift
let storageRef = Storage.storage().reference().child("uploads/photo.jpg") let data = Data() storageRef.putData(data, metadata: nil) { metadata, error in if let error = error { print("Upload error: \(error.localizedDescription)") } else { print("Upload success") } }
Attempts:
2 left
💡 Hint
Consider what happens if you upload empty data.
✗ Incorrect
Uploading empty Data() results in no content, which usually causes the upload to fail or be rejected by Firebase Storage.
expert
2:30remaining
Navigating After Cloud Storage Upload Completion
In a SwiftUI app, you want to upload a file to Firebase Storage and then navigate to a 'SuccessView' only after the upload completes successfully. Which approach correctly achieves this?
iOS Swift
struct UploadView: View {
@State private var uploadComplete = false
var body: some View {
VStack {
if uploadComplete {
SuccessView()
} else {
Button("Upload File") {
uploadFile()
}
}
}
}
func uploadFile() {
let storageRef = Storage.storage().reference().child("files/data.txt")
let data = "Hello".data(using: .utf8)!
storageRef.putData(data, metadata: nil) { metadata, error in
if error == nil {
DispatchQueue.main.async {
uploadComplete = true
}
}
}
}
}Attempts:
2 left
💡 Hint
Think about updating @State variables from background threads.
✗ Incorrect
Firebase Storage completion handlers run on background threads. Updating @State variables must happen on the main thread, so this code will cause a runtime warning or crash.