36 lines
935 B
Swift
36 lines
935 B
Swift
//
|
|
// ObservableArray.swift
|
|
// YAPS
|
|
//
|
|
// Created by Gerrit Linnemann on 20.09.20.
|
|
// Copyright © 2020 Adawim UG (haftungsbeschränkt). All rights reserved.
|
|
//
|
|
// Infos: https://www.thetopsites.net/article/58523692.shtml
|
|
//
|
|
|
|
import Foundation
|
|
import Combine
|
|
import SwiftUI
|
|
|
|
class ObservableArray<T>: ObservableObject {
|
|
|
|
@Published var array:[T] = []
|
|
var cancellables = [AnyCancellable]()
|
|
|
|
init(array: [T]) {
|
|
self.array = array
|
|
}
|
|
|
|
func observeChildrenChanges<T: ObservableObject>() -> ObservableArray<T> {
|
|
let array2 = array as! [T]
|
|
array2.forEach({
|
|
let c = $0.objectWillChange.sink(receiveValue: { _ in self.objectWillChange.send() })
|
|
|
|
// Important: You have to keep the returned value allocated,
|
|
// otherwise the sink subscription gets cancelled
|
|
self.cancellables.append(c)
|
|
})
|
|
return self as! ObservableArray<T>
|
|
}
|
|
}
|