DatabasePublished

@propertyWrapper
public class DatabasePublished<Output> : Publisher

DatabasePublished is a property wrapper whose value automatically changes, on the main queue, when the database is modified.

Usage:

class MyModel {
    // A DatabasePublishers.Value
    static let playersPublisher = Player.observationForAll().publisher(in: dbQueue)

    @DatabasePublished(playersPublisher)
    var players: Result<[Players], Error>
}

let model = MyModel()
try model.players.get() // [Player]
model.$players          // Publisher of output [Player], failure Error

By default, the initial value of the property is immediately fetched from the database. This blocks your main queue until the database access completes.

You can opt in for an asynchronous fetching by providing an initial value:

class MyModel {
    // The initialValue argument triggers asynchronous fetching
    @DatabasePublished(initialValue: [], playersPublisher)
    var players: Result<[Players], Error>
}

let model = MyModel()
// Empty array until the initial fetch is performed
try model.players.get()

DatabasePublished is a reference type which tracks changes the database during its whole life time. It is not advised to use it in a value type such as a struct.

  • Declaration

    Swift

    public typealias Output = Output
  • Declaration

    Swift

    public typealias Failure = Error
  • The freshest value.

    Warning

    this property is not thread-safe and must be used from the main queue only.

    Declaration

    Swift

    public var wrappedValue: Result<Output, Error> { get }
  • Undocumented

    Declaration

    Swift

    public var projectedValue: DatabasePublished<Output> { get }
  • A publisher that publishes an event immediately before the wrapped value changes.

    Warning

    The type of this property will change. Only rely on the fact that it is a Publisher.

    Declaration

    Swift

    public let objectWillChange: PassthroughSubject<Void, Never>
  • Creates a property wrapper whose value automatically changes when the database is modified.

    It must be instantiated from the main queue, or you will get a fatal error.

    Its value is eventually updated on the main queue after each database change.

    Declaration

    Swift

    public convenience init(_ publisher: DatabasePublishers.Value<Output>)
  • Creates a property wrapper whose value automatically changes when the database is modified.

    Its value is eventually updated on the main queue after each database change.

    Declaration

    Swift

    public convenience init(initialValue: Output, _ publisher: DatabasePublishers.Value<Output>)