I'm working on a project where I need to listen to any changes in an array of objects. By changes I refer:
- Add/Remove element in the array
- Any change in the existing array elements
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,
- A new
DownloadTask
instance is added intodownloadTasks
array - An existing
DownloadTask
instance is removed fromdownloadTasks
array - The underlying
DownloadState
is changed for a particularDownloadTask
indownloadTasks
array