Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Documentation/MVVMDemo/MVVMDemo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,7 @@
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 1;
DEVELOPMENT_ASSET_PATHS = "\"MVVMDemo/Preview Content\"";
DEVELOPMENT_TEAM = "";
ENABLE_PREVIEWS = YES;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_KEY_UIApplicationSceneManifest_Generation = YES;
Expand Down Expand Up @@ -489,6 +490,7 @@
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 1;
DEVELOPMENT_ASSET_PATHS = "\"MVVMDemo/Preview Content\"";
DEVELOPMENT_TEAM = "";
ENABLE_PREVIEWS = YES;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_KEY_UIApplicationSceneManifest_Generation = YES;
Expand Down
2 changes: 1 addition & 1 deletion Documentation/QueryDemo/QueryDemo/Views/AppView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ struct AppView: View {
var id: Int64
}

@Query(AnyPlayerRequest())
@Query(AnyPlayerRequest(), printPublisherConfiguration: .init(prefix: "\(#function)"))
private var player: Player?

@State private var editedPlayer: EditedPlayer?
Expand Down
21 changes: 15 additions & 6 deletions Sources/GRDBQuery/Query+DatabaseContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,12 @@ extension Query where Request.Context == DatabaseContext {
///
/// - parameter request: An initial ``Queryable`` request.
public init(
_ request: Request
_ request: Request,
printPublisherConfiguration: QueryPublisherPrintConfiguration? = nil
) {
self.init(request, in: \.databaseContext)
self.init(request,
in: \.databaseContext,
printPublisherConfiguration: printPublisherConfiguration)
}

/// Creates a `Query` that feeds from the `databaseContext`
Expand All @@ -50,9 +53,12 @@ extension Query where Request.Context == DatabaseContext {
///
/// - parameter request: A ``Queryable`` request.
public init(
constant request: Request
constant request: Request,
printPublisherConfiguration: QueryPublisherPrintConfiguration? = nil
) {
self.init(constant: request, in: \.databaseContext)
self.init(constant: request,
in: \.databaseContext,
printPublisherConfiguration: printPublisherConfiguration)
}

/// Creates a `Query` that feeds from the `databaseContext`
Expand Down Expand Up @@ -88,8 +94,11 @@ extension Query where Request.Context == DatabaseContext {
///
/// - parameter request: A SwiftUI binding to a ``Queryable`` request.
public init(
_ request: Binding<Request>
_ request: Binding<Request>,
printPublisherConfiguration: QueryPublisherPrintConfiguration? = nil
) {
self.init(request, in: \.databaseContext)
self.init(request,
in: \.databaseContext,
printPublisherConfiguration: printPublisherConfiguration)
}
}
21 changes: 15 additions & 6 deletions Sources/GRDBQuery/Query+VoidContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,11 @@ extension Query where Request.Context == Void {
/// ```
///
/// - parameter request: An initial ``Queryable`` request.
public init(_ request: Request) {
self.init(request, in: \.void)
public init(_ request: Request,
printPublisherConfiguration: QueryPublisherPrintConfiguration? = nil) {
self.init(request,
in: \.void,
printPublisherConfiguration: printPublisherConfiguration)
}

/// Creates a `Query`, given a SwiftUI binding to a ``Queryable``
Expand Down Expand Up @@ -73,8 +76,11 @@ extension Query where Request.Context == Void {
/// ```
///
/// - parameter request: A SwiftUI binding to a ``Queryable`` request.
public init(_ request: Binding<Request>) {
self.init(request, in: \.void)
public init(_ request: Binding<Request>,
printPublisherConfiguration: QueryPublisherPrintConfiguration? = nil) {
self.init(request,
in: \.void,
printPublisherConfiguration: printPublisherConfiguration)
}

/// Creates a `Query`, given a ``Queryable`` request that uses
Expand Down Expand Up @@ -109,8 +115,11 @@ extension Query where Request.Context == Void {
/// ```
///
/// - parameter request: A ``Queryable`` request.
public init(constant request: Request) {
self.init(constant:request, in: \.void)
public init(constant request: Request,
printPublisherConfiguration: QueryPublisherPrintConfiguration? = nil) {
self.init(constant:request,
in: \.void,
printPublisherConfiguration: printPublisherConfiguration)
}
}

Expand Down
58 changes: 48 additions & 10 deletions Sources/GRDBQuery/Query.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
import Combine
import SwiftUI

public struct QueryPublisherPrintConfiguration {
public let prefix: String
public let outputStream: (any TextOutputStream)?

public init(prefix: String = "", outputStream: (any TextOutputStream)? = nil) {
self.prefix = prefix
self.outputStream = outputStream
}
}

// See Documentation.docc/Extensions/Query.md
@propertyWrapper
@MainActor public struct Query<Request: Queryable> {
Expand All @@ -22,7 +32,7 @@ import SwiftUI

/// The `Query` configuration.
private let configuration: Configuration

private let printPublisherConfiguration: QueryPublisherPrintConfiguration?
/// The last published database value.
public var wrappedValue: Request.Value {
tracker.value ?? Request.defaultValue
Expand Down Expand Up @@ -73,10 +83,12 @@ import SwiftUI
/// - parameter keyPath: A key path to the database in the environment.
public init(
_ request: Request,
in keyPath: KeyPath<EnvironmentValues, Request.Context>)
in keyPath: KeyPath<EnvironmentValues, Request.Context>,
printPublisherConfiguration: QueryPublisherPrintConfiguration? = nil)
{
self._database = Environment(keyPath)
self.configuration = .initial(request)
self.init(configuration: .initial(request),
in: keyPath,
printPublisherConfiguration: printPublisherConfiguration)
}

/// Creates a `Query`, given a ``Queryable`` request, and a key path to
Expand Down Expand Up @@ -111,10 +123,12 @@ import SwiftUI
/// - parameter keyPath: A key path to the database in the environment.
public init(
constant request: Request,
in keyPath: KeyPath<EnvironmentValues, Request.Context>)
in keyPath: KeyPath<EnvironmentValues, Request.Context>,
printPublisherConfiguration: QueryPublisherPrintConfiguration? = nil)
{
self._database = Environment(keyPath)
self.configuration = .constant(request)
self.init(configuration: .constant(request),
in: keyPath,
printPublisherConfiguration: printPublisherConfiguration)
}

/// Creates a `Query`, given a SwiftUI binding to its ``Queryable``
Expand Down Expand Up @@ -158,12 +172,25 @@ import SwiftUI
/// - parameter keyPath: A key path to the database in the environment.
public init(
_ request: Binding<Request>,
in keyPath: KeyPath<EnvironmentValues, Request.Context>)
in keyPath: KeyPath<EnvironmentValues, Request.Context>,
printPublisherConfiguration: QueryPublisherPrintConfiguration? = nil)
{
self.init(configuration: .binding(request),
in: keyPath,
printPublisherConfiguration: printPublisherConfiguration)
}

private init(
configuration: Configuration,
in keyPath: KeyPath<EnvironmentValues, Request.Context>,
printPublisherConfiguration: QueryPublisherPrintConfiguration?)
{
self._database = Environment(keyPath)
self.configuration = .binding(request)
self.configuration = configuration
self.printPublisherConfiguration = printPublisherConfiguration
}


/// A wrapper of the underlying `Query` that creates bindings to
/// its ``Queryable`` request.
///
Expand Down Expand Up @@ -252,8 +279,10 @@ import SwiftUI
func update(
queryObservationEnabled: Bool,
configuration queryConfiguration: Configuration,
printPublisherConfiguration: QueryPublisherPrintConfiguration?,
database: Request.Context)
{

// Give up if observation is disabled
guard queryObservationEnabled else {
trackedRequest = nil
Expand Down Expand Up @@ -291,9 +320,17 @@ import SwiftUI
return
}

var finalPublisher = publisher.eraseToAnyPublisher()

if let printConfiguration = printPublisherConfiguration {
finalPublisher = finalPublisher.print(printConfiguration.prefix,
to: printConfiguration.outputStream)
.eraseToAnyPublisher()
}

// Start tracking the new request
var isUpdating = true
cancellable = publisher.sink(
cancellable = finalPublisher.sink(
receiveCompletion: { [weak self] completion in
guard let self = self else { return }
MainActor.assumeIsolated {
Expand Down Expand Up @@ -335,6 +372,7 @@ extension Query: DynamicProperty {
tracker.update(
queryObservationEnabled: queryObservationEnabled,
configuration: configuration,
printPublisherConfiguration: printPublisherConfiguration,
database: database)
}
}
Expand Down