Overview
TheFileUploadTask class represents a pending or in-progress upload in the upload queue. It tracks upload progress, status, and provides cancellation support. This is an @Observable class designed for use with SwiftUI’s observation framework.
Properties
UUID
required
Unique identifier for this upload task
String
required
Name of the file being uploaded
Int64
required
Size of the file in bytes
URL
required
Local file system URL of the file to upload
Double
default:"0.0"
Upload progress from
0.0 (not started) to 1.0 (completed)Status
default:".pending"
Current status of the upload task. See Status Enum below
String?
Error message if the upload failed. Set when
status is .failedURL?
Public URL of the uploaded object. Set when
status is .completedString?
Optional custom R2 key for the upload. When set, this is used as the full R2 key instead of generating a random-prefix key. Used for folder-aware uploads from the browser
Data?
Security-scoped bookmark for a parent folder. Used when uploading folders to maintain access across app launches
Data?
Security-scoped bookmark for the file itself. Used when uploading individual files via file picker to maintain access across app launches
Task<Void, Never>?
Reference to the running Swift concurrency Task. Held so it can be cancelled via the
cancel() methodStatus Enum
Methods
cancel()
Cancels the upload task if it’s currently running. Behavior:- Cancels the Swift concurrency Task
- Clears the
uploadTaskreference - Sets
statusto.cancelled
Source Code
UploadTask.swift
Protocols
@Observable- SwiftUI observation macro for automatic UI updatesIdentifiable- Has a uniqueidproperty for use in SwiftUI lists
Usage Example
Security-Scoped Bookmarks
TheparentFolderBookmark and fileBookmark properties store security-scoped bookmark data for macOS sandbox compliance:
- parentFolderBookmark: Used when uploading entire folders, maintains access to the parent directory
- fileBookmark: Used when uploading individual files via file picker, maintains access to the specific file
Notes
- This is a reference type (
class) rather than a value type (struct) because it needs to be mutated during upload - The
@Observablemacro enables automatic SwiftUI view updates when properties change - The
uploadTaskproperty stores a reference to the Swift concurrency Task to enable cancellation - Custom upload keys (
uploadKey) are used for maintaining folder structure in browser-based uploads