Protocols

The following protocols are available globally.

  • Protocol defining the interface for heavy task data providers.

    This protocol establishes the contract for implementing concrete data providers that execute resource-intensive operations. All data providers must implement both start and stop methods to ensure proper task lifecycle management and integration with the task manager.

    Core Responsibilities

    • Task Execution: Implement the actual heavy computation or I/O operation
    • Progress Reporting: Publish real-time updates through the event system
    • Interruption Handling: Gracefully respond to stop requests from the manager
    • State Management: Maintain task state and support resumption when applicable
    • Error Handling: Properly handle and report failures through the result system

    Implementation Guidelines

    • Thread Safety: Methods may be called from different threads; ensure proper synchronization
    • Cancellation: Check for interruption periodically during long-running operations
    • Resource Management: Clean up resources properly in both success and failure cases
    • Event Publishing: Use customEventPublisher for progress updates, not result delivery
    • Result Delivery: Call resultPublisher exactly once with the final outcome

    Integration with Task Manager

    The task manager handles:

    • Provider instantiation and lifecycle management
    • Event routing to registered observers
    • Result caching and callback coordination
    • Concurrency control and queue management
    • Stop action processing (reuse vs dealloc)

    Example Implementation Pattern

    class MyHeavyTaskProvider: KVHeavyTaskBaseDataProvider<String, Data, Progress>, KVHeavyTaskDataProviderInterface {
        private var isStopped = false
    
        func start() {
            // Validate input and setup
            guard isValidKey(key) else {
                resultPublisher(.failure(ValidationError.invalidKey))
                return
            }
    
            // Execute task with progress updates
            performHeavyOperation { progress in
                guard !isStopped else { return }
                customEventPublisher(progress)
            } completion: { result in
                resultPublisher(result)
            }
        }
    
        func stop() -> KVHeavyTaskDataProviderStopAction {
            isStopped = true
            return shouldPreserveState ? .reuse : .dealloc
        }
    }
    
    See more

    Declaration

    Swift

    public protocol KVHeavyTaskDataProviderInterface