Tiercel Documentation

repository·master·Indexed 25 days ago

https://github.com/danie1s/tiercel

A production-oriented iOS download framework written in pure Swift. Tiercel provides high-level orchestration for background downloads, relaunch recovery, and resumable transfers using Apple's native URLSession stack. It includes features for batch downloads, file integrity validation via checksums (e.g., MD5), and network policy configuration through SessionConfiguration.

Tokens
2.4K
Snippets
12
Records
14
Agent score
34%

What's inside Tiercel

  1. Install Tiercel via CocoaPods

    master

    Add Tiercel to your Podfile. Ensure your platform is set to ios with version 12.0 or higher and use_frameworks! is enabled.

    platform :ios, '12.0'
    use_frameworks!
    
    target 'YourTargetName' do
      pod 'Tiercel'
    end

    Then run:

    pod install
  2. Quick Start: Download a file with SessionManager

    master

    To start downloading, create a SessionConfiguration, initialize a SessionManager with a unique identifier, and call .download(url). You can then attach callbacks for progress, success, or failure.

    import Tiercel
    
    var configuration = SessionConfiguration()
    configuration.allowsCellularAccess = true
    configuration.maxConcurrentTasksLimit = 3
    
    let manager = SessionManager("downloads", configuration: configuration)
    
    let task = manager.download("https://example.com/video.mp4")
    
    task?.progress(onMainQueue: true) { task in
        print("progress:", task.progress.fractionCompleted)
    }.success { task in
        print("saved to:", task.filePath)
    }.failure { _ in
        print("download failed")
    }
  3. Handle background downloads and relaunch recovery

    master

    Tiercel persists task metadata and resume data to disk to support recovery after app restarts. To support native background session callbacks, you must pass the completion handler from AppDelegate to the corresponding SessionManager using its identifier.

    let downloadManagers = [managerA, managerB]
    
    func application(_ application: UIApplication,
                     handleEventsForBackgroundURLSession identifier: String,
                     completionHandler: @escaping () -> Void) {
        for manager in downloadManagers where manager.identifier == identifier {
            manager.completionHandler = completionHandler
            break
        }
    }
  4. Configure Network Policy with SessionConfiguration

    master

    Use SessionConfiguration to control network access permissions and concurrency limits for a SessionManager.

    var configuration = SessionConfiguration()
    configuration.maxConcurrentTasksLimit = 3
    configuration.allowsCellularAccess = true
    configuration.allowsConstrainedNetworkAccess = true
    configuration.allowsExpensiveNetworkAccess = true
    
    let manager = SessionManager("downloads", configuration: configuration)
  5. Configure network policies with SessionConfiguration

    master

    Use SessionConfiguration to define how tasks behave regarding network constraints. Available properties include:

    • maxConcurrentTasksLimit: Maximum number of concurrent tasks.
    • allowsCellularAccess: Whether to allow cellular data.
    • allowsConstrainedNetworkAccess: Whether to allow constrained networks.
    • allowsExpensiveNetworkAccess: Whether to allow expensive networks.
    var configuration = SessionConfiguration()
    configuration.maxConcurrentTasksLimit = 3
    configuration.allowsCellularAccess = true
    configuration.allowsConstrainedNetworkAccess = true
    configuration.allowsExpensiveNetworkAccess = true
    
    let manager = SessionManager("downloads", configuration: configuration)
  6. Control downloads by URL or Task instance

    master

    You can manage active downloads using either the source URL or the returned task instance via the SessionManager methods: start, suspend, cancel, and remove.

    let url = "https://example.com/video.mp4"
    
    manager.start(url)
    manager.suspend(url)
    manager.cancel(url)
    manager.remove(url, completely: false)
    
    if let task = task {
        manager.start(task)
        manager.suspend(task)
        manager.cancel(task)
        manager.remove(task, completely: false)
    }
  7. Quick Start: Download a file with progress and success callbacks

    master

    Initialize a SessionConfiguration, create a SessionManager with a unique identifier, and use the .download(_:) method to start a task. You can chain .progress and .success/.failure handlers.

    import Tiercel
    
    var configuration = SessionConfiguration()
    configuration.allowsCellularAccess = true
    configuration.maxConcurrentTasksLimit = 3
    
    let manager = SessionManager("downloads", configuration: configuration)
    
    let task = manager.download("https://example.com/video.mp4")
    
    task?.progress(onMainQueue: true) { task in
        print("progress:", task.progress.fractionCompleted)
    }.success { task in
        print("saved to:", task.filePath)
    }.failure { _ in
        print("download failed")
    }
  8. Perform batch downloads

    master

    Use multiDownload(_:) on a SessionManager to create multiple download tasks from an array of URLs simultaneously.

    let urls = [
        "https://example.com/episode-1.mp4",
        "https://example.com/episode-2.mp4"
    ]
    
    let tasks = manager.multiDownload(urls)
    print(tasks.count)
  9. Manage download tasks via SessionManager

    master

    You can control downloads using either the URL or the task instance directly through the SessionManager methods: start, suspend, cancel, and remove.

    let url = "https://example.com/video.mp4"
    
    // Using URL
    manager.start(url)
    manager.suspend(url)
    manager.cancel(url)
    manager.remove(url, completely: false)
    
    // Using Task instance
    if let task = task {
        manager.start(task)
        manager.suspend(task)
        manager.cancel(task)
        manager.remove(task, completely: false)
    }