Reliable photo uploads

Networking Senior Swift 5.5+ iOS 16+ Reviewed By Alok Singh

Pick the fix

Uploads must continue after the user leaves the app. Which call?

Options
Hint 1 of 1

What do background sessions need?

Choose an answer, then open Show answer below to check it.

Show answer

Answer: A backgroundSession.uploadTask(with: request, fromFile: fileURL)

Correct. File uploads in a background session continue while the app is suspended.

Why not the others?
  • B backgroundSession.uploadTask(with: request, from: data). Background sessions don't support in-memory uploads.
  • C try await URLSession.shared.upload(for: request, from: data) in a Task. The task stops when the app is suspended.
  • D Build the multipart body in memory and send it on app launch. Memory heavy, and it doesn't survive backgrounding.

30-second answer

Write each photo to a file and queue an upload with a background URLSession uploadTask(with:fromFile:), which keeps going while the app is suspended. Persist the queue with an idempotency key per photo so retries don't duplicate. Compress or resize first, upload in chunks or resumable sessions for big files, and show per-item progress and failures.

Practise saying it · 60 s. No recording, just a timer

Design reliable photo uploads.

Say your answer out loud, then check which of these points you covered:

  • Background file uploads
  • Persistent queue
  • Idempotency
  • Progress and retry

Deep dive

Background sessions support only file-based uploads, not in-memory Data.

Store queue state in a database so a relaunch can reconcile.

Use a resumable upload protocol (such as tus or the server's chunk API) for large videos.

Respect the user's cellular and Low Data Mode choices.

Common mistakes

  • Uploading Data from memory in a background session.
  • Retrying without an idempotency key.

Senior take & follow-ups

Seniors think about persistence, idempotency and resumability, not just the request.

Follow-up questions

Think of your answer first, then open the hint.

  1. Why idempotency keys?

    Show hint

    A retry after a lost response mustn't create a second photo.

  2. What happens if the app is terminated?

    Show hint

    The system continues and relaunches you to handle events.

Suggest a fix if something here is wrong or out of date.