0
0
iOS Swiftmobile~20 mins

Cloud Storage in iOS Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Cloud Storage Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2: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")
  }
}
AThe file is uploaded to the root of Firebase Storage, ignoring the 'images/' folder.
BThe file at the URL is uploaded to the path 'images/image.png' in Firebase Storage, and 'Upload successful' is printed on success.
CThe file is uploaded but the completion handler is never called, so no message is printed.
DThe code will crash because URL(string:) returns an optional and is not safely unwrapped.
Attempts:
2 left
💡 Hint
Check how the URL is created and how the completion handler works.
🧠 Conceptual
intermediate
1:30remaining
Understanding Cloud Storage Security Rules
Which statement correctly describes Firebase Storage security rules behavior?
ASecurity rules only apply to Firestore database, not to Firebase Storage.
BSecurity rules automatically encrypt files on the device before uploading to Firebase Storage.
CSecurity rules control who can read or write files in Firebase Storage based on authentication and file path.
DSecurity rules allow anyone to access files unless you manually disable public access in the Firebase console.
Attempts:
2 left
💡 Hint
Think about how access is controlled in cloud storage.
lifecycle
advanced
2: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?
ACall downloadTask?.pause() to pause and downloadTask?.resume() to resume the download.
BCall downloadTask?.cancel() to pause and create a new downloadTask to resume.
CCall downloadTask?.suspend() to pause and downloadTask?.start() to resume.
DDownload tasks cannot be paused or resumed once started.
Attempts:
2 left
💡 Hint
Check the StorageDownloadTask API for controlling downloads.
🔧 Debug
advanced
2: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")
  }
}
AThe data is empty, so the upload fails because there is no content to upload.
BThe storage reference path is invalid because it uses a forward slash.
CThe metadata parameter is nil, which causes a runtime error.
DThe completion handler is missing, so the upload never completes.
Attempts:
2 left
💡 Hint
Consider what happens if you upload empty data.
navigation
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
        }
      }
    }
  }
}
AYou must use a NavigationLink with isActive bound to uploadComplete to navigate properly.
BThis code works perfectly and navigates to SuccessView after upload completes.
CThe uploadFile function should be marked async and awaited to update uploadComplete correctly.
DThis code will not compile because uploadComplete is modified inside a closure without @MainActor or DispatchQueue.main.async.
Attempts:
2 left
💡 Hint
Think about updating @State variables from background threads.