• Stars
    star
    216
  • Rank 183,179 (Top 4 %)
  • Language
    Swift
  • License
    MIT License
  • Created about 3 years ago
  • Updated 5 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

The SwiftUI companion for GRDB

GRDBQuery

Latest release: March 25, 2023 • version 0.7.0CHANGELOG

Requirements: iOS 14.0+ / macOS 11+ / tvOS 14.0+ / watchOS 7.0+ • Swift 5.7+ / Xcode 14+

📖 Documentation


This library helps building SwiftUI applications that access "services", such as a local database, through the SwiftUI environment.

Its main purpose is to help users of the GRDB SQLite toolkit. Yet GRDBQuery has no dependency on GRDB: you can use it in other contexts, in a Core Data or Realm application, and generally in any kind of app, as long as you'd like to put the SwiftUI environment to good use.

What's in the Box?

GRDBQuery provides two property wrappers:

  • With @Query, SwiftUI views can automatically update their content when the database changes. Generally speaking, @Query helps subscribing to Combine publishers defined from the SwiftUI environment:

    /// 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) }
        }
    }
  • With @EnvironmentStateObject, applications can build observable objects that find their dependencies in the SwiftUI environment:

    /// A view that displays the list of players provided by its view model
    struct PlayerList: View {
        @EnvironmentStateObject var viewModel: PlayerListViewModel
        
        init() {
            _viewModel = EnvironmentStateObject { env in
                PlayerListViewModel(database: env.database)
            }
        }
        
        var body: some View {
            List(viewModel.players) { player in Text(player.name) }
        }
    }

    @EnvironmentStateObject is a general-purpose property wrapper, akin to the SwiftUI @Environment, @EnvironmentObject, @ObservedObject, and @StateObject.

    It fits well the view models of the MVVM architecture, as well as dependency injection.

Both property wrappers can work together, so that developers can run quick experiments, build versatile previews, and also apply strict patterns and conventions. Pick @Query, or @EnvironmentStateObject, depending on your needs!

Why GRDBQuery?

GRDBQuery makes sure SwiftUI views are immediately rendered with the 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, the GRDBQuery property wrappers have you fully covered.

Documentation

Learn how to use @Query and @EnvironmentStateObject in the Documentation.

Check out the GRDBQuery demo apps, and the GRDB demo apps for various examples.

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! @EnvironmentStateObject was later introduced because @Query does not fit the MVVM architecture. The author sees benefits in both property wrappers.

More Repositories

1

GRDB.swift

A toolkit for SQLite databases, with a focus on application development
Swift
6,844
star
2

GRMustache

Flexible and production-ready Mustache templates for MacOS Cocoa and iOS
Objective-C
1,443
star
3

GRMustache.swift

Flexible Mustache templates for Swift
Swift
598
star
4

Semaphore

A Synchronization Primitive for Swift Concurrency
Swift
511
star
5

CombineExpectations

Utilities for tests that wait for Combine publishers
Swift
251
star
6

GRDBCombine

GRDB ❤️ Combine
223
star
7

SortedDifference

A general sequence diffing algorithm, with a very specific purpose
Swift
56
star
8

GRDBSnapshotTesting

The snapshot testing library for GRDB
Swift
32
star
9

CombineTraits

Combine Publishers with Guarantees
Swift
25
star
10

GRDBObjc

FMDB-compatible bindings to GRDB.swift
Objective-C
20
star
11

WWDCCompanion

A demo app for full text search with GRDB.swift
Swift
18
star
12

GRDBCollections

Support for large result sets with GRDB
Swift
9
star
13

GRDBDemo

An iOS app that demonstrates the SQLite library GRDB and its FetchedRecordsController
9
star
14

GRValidation

Validation toolkit for Swift 2
Swift
8
star
15

GRDBDiff

Diff algorithms for SQLite, based on GRDB
Swift
5
star
16

CocoaHeadsDemo

Swift
4
star
17

GRMustacheSPM

A Swift package that uses GRMustache.swift
Swift
2
star
18

CSQLite

C
1
star
19

ARTemplating

Very simplistic performance comparison of mustache.js in a UIWebView and GRMustache
Objective-C
1
star
20

GRDBIssue636

Reproducible crash reported in https://github.com/groue/GRDB.swift/issues/636
Swift
1
star
21

SE-0235

A playground which explores SE-0235: https://forums.swift.org/t/se-0235-add-result-to-the-standard-library/17752
Swift
1
star
22

GRMustacheBenchmark

GRMustache Benchmarks
Objective-C
1
star