I was wondering if this is safe and correct:
extension Optional: ThreadConfined where Wrapped: ThreadConfined {
public var realm: RealmSwift.Realm? {
self?.realm
}
public var isInvalidated: Bool {
self?.isInvalidated ?? true
}
public var isFrozen: Bool {
self?.isFrozen ?? true
}
public func freeze() -> Optional<Wrapped> {
self?.freeze()
}
public func thaw() -> Optional<Wrapped>? {
self?.thaw()
}
}
The reason is I want to be able to do this:
public extension Published.Publisher {
func observe<O>(_ keyPaths: O.KeyPaths...) -> AnyPublisher<ResultID<O>, Never>
where O: ThreadConfined & MSObject, Output == O?
{
self.freeze()
.threadSafeReference()
.receive(on: DispatchQueue.main)
.buffer(size: 1, prefetch: .byRequest, whenFull: .dropOldest)
.map { ResultID(id: $0?.id) }
.eraseToAnyPublisher()
}
}
But freeze()
only works on threadconfined types. (By the way, if the above observe method is incorrect I’d also love to hear it).