I'm trying to mimic the Combine @Published property wrapper. My end goal is to create a new custom property wrapper (e.g. @PublishedAppStorage) of @Published with a nested @AppStorage. So I've started just by trying to mimic the @Published.
My problem that it crashes when accessing the original value from within the sink block with the error:
Thread 1: Simultaneous accesses to 0x600000103328, but modification requires exclusive access
I've spent days trying to find a way.
Here is my custom @DMPublished:
@propertyWrapper
struct DMPublished<Value> {
private let subject:CurrentValueSubject<Value, Never>
init(wrappedValue: Value) {
self.wrappedValue = wrappedValue
self.subject = CurrentValueSubject(wrappedValue)
}
var wrappedValue: Value {
willSet {
subject.send(newValue)
}
}
var projectedValue: AnyPublisher<Value, Never> {
subject.eraseToAnyPublisher()
}
}
The ObservableObject defining my properties:
import Combine
public class DMDefaults: ObservableObject {
static public let shared = DMDefaults()
private init(){}
@Published public var corePublishedString = "dd"
@DMPublished public var customPublishedString = "DD"
}
And here is my test function:
public func testSink()
{
let gdmDefaults = DMDefaults.shared
gdmDefaults.corePublishedString = "ee"; gdmDefaults.customPublishedString = "EE"
gdmDefaults.corePublishedString = "ff"; gdmDefaults.customPublishedString = "FF"
let coreSub = gdmDefaults.$corePublishedString.sink { (newVal) in
print("coreSub: oldVal=\(gdmDefaults.corePublishedString) ; newVal=\(newVal)")
}
let custSub = gdmDefaults.$customPublishedString.sink { (newVal) in
print("custSub: oldVal=\(gdmDefaults.customPublishedString) ; newVal=\(newVal)") // **Crashing here**
}
gdmDefaults.corePublishedString = "gg"; gdmDefaults.customPublishedString = "GG"
}
Will appreciate any help here... thanks...