I'm working on a project where I need to listen to any changes in an array of objects. By changes I refer:
Here is the sample code,
enum DownloadState {
case queued
case completed
}
class DownloadTask: ObservableObject {
var downloadState: DownloadState = .queued
}
class DownloadManager {
var downloadTasks = [DownloadTask]()
}
In the above code, DownloadManager
contains an array of DownloadTask
. I want to listen to the changes when,
DownloadTask
instance is added into downloadTasks
arrayDownloadTask
instance is removed from downloadTasks
arrayDownloadState
is changed for a particular DownloadTask
in downloadTasks
arrayThe approach that worked for us:
1. Create a protocol to listen to any changes in the DownloadTask
public protocol DownloadStateHandlerDelegate: AnyObject {
func didUpdateDownloadState(title: String, downloadState: DownloadState)
}
2. Passed the delegate
instance to the DownloadManager
during initialization.
class DownloadManager {
private weak var delegate: DownloadStateHandlerDelegate?
private var downloadTasks = [DownloadTask]()
init(delegate: DownloadStateHandlerDelegate) {
self.delegate = delegate
}
}
3. Called the delegate
method didUpdateDownloadState
whenever the downloadState
of a DownloadTask
is updated.
Example:
class DownloadTask {
@Published private var downloadState: DownloadState {
didSet {
self.delegate?.didUpdateDownloadState(title: self.title, downloadState: self.downloadState)
}
}
}