Classes

The following classes are available globally.

  • 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.

    See more

    Declaration

    Swift

    @propertyWrapper
    public class DatabasePublished<Output> : Publisher