Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize a reference to the Firebase Storage root.
iOS Swift
let storage = Storage.storage()
let storageRef = storage.[1] Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'ref()' instead of 'reference()'.
Trying to use 'root()' which does not exist.
✗ Incorrect
The correct method to get the root reference of Firebase Storage is reference().
2fill in blank
mediumComplete the code to upload data to a file named "image.jpg" in Firebase Storage.
iOS Swift
let data = Data() let imageRef = storageRef.child("[1]") imageRef.putData(data, metadata: nil) { metadata, error in // handle completion }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'image.png' instead of 'image.jpg'.
Using a different file name like 'photo.jpg'.
✗ Incorrect
The file name to upload is 'image.jpg' as specified in the instruction.
3fill in blank
hardFix the error in the code to download data from Firebase Storage with a maximum size of 1MB.
iOS Swift
let imageRef = storageRef.child("image.jpg") imageRef.getData(maxSize: [1]) { data, error in if let data = data { // use data } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 1024 which is only 1KB.
Using 1_000_000 which is close but not exact bytes.
✗ Incorrect
The maxSize parameter expects bytes. 1MB equals 1024 * 1024 bytes.
4fill in blank
hardFill both blanks to create a dictionary of file names and their sizes in bytes from metadata.
iOS Swift
var fileSizes = [String: Int64]() for item in items { item.getMetadata { metadata, error in if let metadata = metadata { fileSizes[[1]] = metadata.[2] } } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'item.fullPath' instead of 'item.name' for the file name.
Using 'totalBytes' instead of 'size' for the file size.
✗ Incorrect
The file name is accessed by item.name and the size in bytes is metadata.size.
5fill in blank
hardFill all three blanks to delete a file named "oldfile.txt" from Firebase Storage and handle errors.
iOS Swift
let oldFileRef = storageRef.child("[1]") oldFileRef.[2] { error in if let error = error { print("Failed to delete: \(error.localizedDescription)") } else { print("[3]") } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'delete(completion:)' which is not the correct method call syntax.
Using a wrong file name or success message.
✗ Incorrect
The file name is 'oldfile.txt', the method to delete is 'delete', and the success message is 'File deleted successfully'.