Swiftpack.co - groue/GRDBQuery as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by groue.
groue/GRDBQuery 0.3.0
The SwiftUI companion for GRDB
⭐️ 56
🕓 2 weeks ago
iOS macOS watchOS tvOS
.package(url: "https://github.com/groue/GRDBQuery.git", from: "0.3.0")

GRDBQuery

Latest release: May 1, 2022 • version 0.3.0CHANGELOG

Requirements: iOS 13.0+ / macOS 10.15+ / tvOS 13.0+ / watchOS 6.0+ • Swift 5.5+ / Xcode 13+


This package provides the @Query property wrapper, that lets your SwiftUI views automatically update their content when the database changes.

import GRDBQuery
import SwiftUI

/// A view that displays an always up-to-date list of players in the database.
struct PlayerList: View {
    @Query(PlayersRequest())
    var players: [Player]
    
    var body: some View {
        List(players) { player in
            Text(player.name)
        }
    }
}

@Query is for GRDB what @FetchRequest is for Core Data. Although @Query does not depend on GRDB, it was designed with GRDB in mind.

Why @Query?

@Query solves a tricky SwiftUI challenge. It makes sure SwiftUI views are immediately rendered with the database content you expect.

For example, when you display a List that animates its changes, you do not want to see an animation for the initial state of the list, or to prevent this undesired animation with extra code.

You also want your SwiftUI previews to display the expected values without having to run them.

Techniques based on onAppear(perform:), onReceive(_:perform) and similar methods suffer from this "double-rendering" problem and its side effects. By contrast, @Query has you fully covered.

Documentation

Learn how to use @Query in the Documentation, or jump straight to the Getting Started guide.

Check out the demo app, and the GRDB demo apps for various examples of @Query uses.

Thanks

🙌 @Query was vastly inspired from Core Data and SwiftUI by @davedelong, with a critical improvement contributed by @steipete. Many thanks to both of you!

GitHub

link
Stars: 56
Last commit: 2 minutes ago
jonrohan Something's broken? Yell at me @ptrpavlik. Praise and feedback (and money) is also welcome.

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics