Bird
Raised Fist0
iOS Swiftmobile~20 mins

Cloud Storage in iOS Swift - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1.

What is the main purpose of Cloud Storage in an iOS app?

easy
A. To create user interface elements
B. To store app settings locally on the device
C. To save files online for easy access and sharing
D. To manage app navigation flow

Solution

  1. Step 1: Understand Cloud Storage role

    Cloud Storage is designed to save files online so users can access and share them easily.
  2. Step 2: Compare options

    Options B, C, and D describe other app functions unrelated to Cloud Storage.
  3. Final Answer:

    To save files online for easy access and sharing -> Option C
  4. Quick Check:

    Cloud Storage = Online file saving [OK]
Hint: Cloud Storage means saving files online, not local data [OK]
Common Mistakes:
  • Confusing Cloud Storage with local storage
  • Thinking it manages UI or navigation
  • Assuming it stores app settings only
2.

Which of the following is the correct way to create a reference to a file named image.png in Firebase Cloud Storage using Swift?

let storage = Storage.storage()
let storageRef = ...
easy
A. storageRef = storage.child("image.png")
B. storageRef = storage.reference().child("image.png")
C. storageRef = Storage.reference("image.png")
D. storageRef = storage.reference("image.png")

Solution

  1. Step 1: Recall Firebase Storage syntax

    To get a reference, use storage.reference() then child("filename") to point to the file.
  2. Step 2: Check each option

    storageRef = storage.reference().child("image.png") correctly chains reference() and child(). Options B, C, and D use incorrect method calls or syntax.
  3. Final Answer:

    storageRef = storage.reference().child("image.png") -> Option B
  4. Quick Check:

    Use reference().child() to get file reference [OK]
Hint: Use reference().child("filename") to get file reference [OK]
Common Mistakes:
  • Calling child() directly on storage
  • Using Storage.reference() instead of storage.reference()
  • Passing filename directly to reference()
3.

What will be printed if the following Swift code successfully uploads a file to Firebase Storage?

let storageRef = Storage.storage().reference().child("file.txt")
let localFile = URL(fileURLWithPath: "/path/to/file.txt")
storageRef.putFile(from: localFile, metadata: nil) { metadata, error in
  if let error = error {
    print("Upload failed: \(error.localizedDescription)")
  } else {
    print("Upload succeeded")
  }
}
medium
A. Upload succeeded
B. Upload failed: File not found
C. Upload failed: Network error
D. No output

Solution

  1. Step 1: Analyze the completion handler

    The closure checks if error is nil; if so, it prints "Upload succeeded".
  2. Step 2: Given successful upload

    Since upload is successful, error is nil, so the else block runs printing "Upload succeeded".
  3. Final Answer:

    Upload succeeded -> Option A
  4. Quick Check:

    Success means error is nil, print "Upload succeeded" [OK]
Hint: If error is nil, upload succeeded message prints [OK]
Common Mistakes:
  • Assuming error is always non-nil
  • Confusing success and failure blocks
  • Ignoring the asynchronous completion handler
4.

Identify the error in this Swift code snippet for uploading a file to Firebase Storage:

let storageRef = Storage.storage().reference().child("doc.pdf")
let fileURL = URL(string: "/documents/doc.pdf")!
storageRef.putFile(from: fileURL, metadata: nil) { metadata, error in
  if error != nil {
    print("Upload error")
  } else {
    print("Upload complete")
  }
}
medium
A. Using URL(string:) instead of URL(fileURLWithPath:)
B. Missing metadata parameter in putFile
C. Incorrect child path syntax
D. No completion handler provided

Solution

  1. Step 1: Check URL creation method

    URL(string:) is for web URLs; local file paths need URL(fileURLWithPath:).
  2. Step 2: Identify impact

    Using URL(string:) with a local path returns nil or incorrect URL, causing upload failure.
  3. Final Answer:

    Using URL(string:) instead of URL(fileURLWithPath:) -> Option A
  4. Quick Check:

    Local file URLs need URL(fileURLWithPath:) [OK]
Hint: Use URL(fileURLWithPath:) for local file paths [OK]
Common Mistakes:
  • Using URL(string:) for local files
  • Forgetting to unwrap optional URLs safely
  • Ignoring completion handler errors
5.

You want to upload multiple images to Firebase Cloud Storage and keep track of their download URLs for later use. Which approach correctly handles this in Swift?

let images = [URL(fileURLWithPath: "/img1.png"), URL(fileURLWithPath: "/img2.png")]
var downloadURLs: [URL] = []
for imageURL in images {
  let ref = Storage.storage().reference().child(imageURL.lastPathComponent)
  ref.putFile(from: imageURL, metadata: nil) { metadata, error in
    if error == nil {
      ref.downloadURL { url, error in
        if let url = url {
          downloadURLs.append(url)
        }
      }
    }
  }
}
print(downloadURLs)
hard
A. The code will cause a syntax error due to missing await
B. This code correctly prints all download URLs immediately
C. The code uploads files but does not get download URLs
D. This code may print an empty array because uploads are asynchronous

Solution

  1. Step 1: Understand asynchronous behavior

    Uploads and downloadURL calls run asynchronously, so print(downloadURLs) runs before completion.
  2. Step 2: Effect on printed output

    Because of async calls, downloadURLs is likely empty when printed immediately after the loop.
  3. Final Answer:

    This code may print an empty array because uploads are asynchronous -> Option D
  4. Quick Check:

    Async uploads delay URL collection, print runs too early [OK]
Hint: Async calls need completion before using results [OK]
Common Mistakes:
  • Expecting synchronous behavior from async uploads
  • Printing results before upload completion
  • Not using dispatch groups or async-await to wait
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard