From 5d69d2a35bdd7395b12ebd7252a13f47430feefb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Tue, 19 Oct 2021 08:54:48 +0200 Subject: [PATCH] All demo apps provide unbounded read-only access to the database Also, UIKit app now does not have to deal with an initial asynchronous delivery of players --- .../GRDBAsyncDemo/AppDatabase.swift | 4 ++ .../GRDBCombineDemo/AppDatabase.swift | 4 ++ .../GRDBDemoiOS/GRDBDemoiOS/AppDatabase.swift | 33 +++------- .../PlayerListViewController.swift | 29 ++++----- .../GRDBDemoiOSTests/AppDatabaseTests.swift | 60 ------------------- 5 files changed, 26 insertions(+), 104 deletions(-) diff --git a/Documentation/DemoApps/GRDBAsyncDemo/GRDBAsyncDemo/AppDatabase.swift b/Documentation/DemoApps/GRDBAsyncDemo/GRDBAsyncDemo/AppDatabase.swift index 4783181ea..aa7115ad9 100644 --- a/Documentation/DemoApps/GRDBAsyncDemo/GRDBAsyncDemo/AppDatabase.swift +++ b/Documentation/DemoApps/GRDBAsyncDemo/GRDBAsyncDemo/AppDatabase.swift @@ -155,6 +155,10 @@ extension AppDatabase { // MARK: - Database Access: Reads +// This demo app does not provide any specific reading method, and instead +// gives an unrestricted read-only access to the rest of the application. +// In your app, you are free to choose another path, and define focused +// reading methods. extension AppDatabase { /// Provides a read-only access to the database var databaseReader: DatabaseReader { diff --git a/Documentation/DemoApps/GRDBCombineDemo/GRDBCombineDemo/AppDatabase.swift b/Documentation/DemoApps/GRDBCombineDemo/GRDBCombineDemo/AppDatabase.swift index fa2007952..6d9d831e8 100644 --- a/Documentation/DemoApps/GRDBCombineDemo/GRDBCombineDemo/AppDatabase.swift +++ b/Documentation/DemoApps/GRDBCombineDemo/GRDBCombineDemo/AppDatabase.swift @@ -156,6 +156,10 @@ extension AppDatabase { // MARK: - Database Access: Reads +// This demo app does not provide any specific reading method, and instead +// gives an unrestricted read-only access to the rest of the application. +// In your app, you are free to choose another path, and define focused +// reading methods. extension AppDatabase { /// Provides a read-only access to the database var databaseReader: DatabaseReader { diff --git a/Documentation/DemoApps/GRDBDemoiOS/GRDBDemoiOS/AppDatabase.swift b/Documentation/DemoApps/GRDBDemoiOS/GRDBDemoiOS/AppDatabase.swift index 3b6a124ab..3abba5a5a 100644 --- a/Documentation/DemoApps/GRDBDemoiOS/GRDBDemoiOS/AppDatabase.swift +++ b/Documentation/DemoApps/GRDBDemoiOS/GRDBDemoiOS/AppDatabase.swift @@ -121,32 +121,13 @@ extension AppDatabase { // MARK: - Database Access: Reads +// This demo app does not provide any specific reading method, and instead +// gives an unrestricted read-only access to the rest of the application. +// In your app, you are free to choose another path, and define focused +// reading methods. extension AppDatabase { - /// Tracks changes in players ordered by name - func observePlayersOrderedByName( - onError: @escaping (Error) -> Void, - onChange: @escaping ([Player]) -> Void) - -> DatabaseCancellable - { - ValueObservation - .tracking(Player.all().orderedByName().fetchAll) - .start( - in: dbWriter, - onError: onError, - onChange: onChange) - } - - /// Tracks changes in players ordered by score - func observePlayersOrderedByScore( - onError: @escaping (Error) -> Void, - onChange: @escaping ([Player]) -> Void) - -> DatabaseCancellable - { - ValueObservation - .tracking(Player.all().orderedByScore().fetchAll) - .start( - in: dbWriter, - onError: onError, - onChange: onChange) + /// Provides a read-only access to the database + var databaseReader: DatabaseReader { + dbWriter } } diff --git a/Documentation/DemoApps/GRDBDemoiOS/GRDBDemoiOS/ViewControllers/PlayerListViewController.swift b/Documentation/DemoApps/GRDBDemoiOS/GRDBDemoiOS/ViewControllers/PlayerListViewController.swift index 3ebd0f87f..ce2339e62 100644 --- a/Documentation/DemoApps/GRDBDemoiOS/GRDBDemoiOS/ViewControllers/PlayerListViewController.swift +++ b/Documentation/DemoApps/GRDBDemoiOS/GRDBDemoiOS/ViewControllers/PlayerListViewController.swift @@ -10,7 +10,6 @@ class PlayerListViewController: UITableViewController { @IBOutlet private weak var newPlayerButtonItem: UIBarButtonItem! private var dataSource: PlayerDataSource! - private var animatesPlayersChange = false // Don't animate first update private var playersCancellable: DatabaseCancellable? private var playerOrdering: PlayerOrdering = .byScore { didSet { @@ -95,35 +94,29 @@ class PlayerListViewController: UITableViewController { var snapshot = NSDiffableDataSourceSnapshot() snapshot.appendSections([0]) snapshot.appendItems(players, toSection: 0) - - if animatesPlayersChange { - dataSource.apply(snapshot, animatingDifferences: true, completion: nil) - } else { - // Future updates will be animated - animatesPlayersChange = true - dataSource.apply(snapshot, animatingDifferences: false, completion: nil) - } + dataSource.apply(snapshot, animatingDifferences: true, completion: nil) } private func observePlayers() { + let request: QueryInterfaceRequest switch playerOrdering { case .byName: - playersCancellable = AppDatabase.shared.observePlayersOrderedByName( - onError: { error in fatalError("Unexpected error: \(error)") }, - onChange: { [weak self] players in - guard let self = self else { return } - self.configureTitle(from: players) - self.configureDataSource(from: players) - }) + request = Player.all().orderedByName() case .byScore: - playersCancellable = AppDatabase.shared.observePlayersOrderedByScore( + request = Player.all().orderedByScore() + } + + playersCancellable = ValueObservation + .tracking(request.fetchAll(_:)) + .start( + in: AppDatabase.shared.databaseReader, + scheduling: .immediate, onError: { error in fatalError("Unexpected error: \(error)") }, onChange: { [weak self] players in guard let self = self else { return } self.configureTitle(from: players) self.configureDataSource(from: players) }) - } } } diff --git a/Documentation/DemoApps/GRDBDemoiOS/GRDBDemoiOSTests/AppDatabaseTests.swift b/Documentation/DemoApps/GRDBDemoiOS/GRDBDemoiOSTests/AppDatabaseTests.swift index 5bc0654f8..58e7abe66 100644 --- a/Documentation/DemoApps/GRDBDemoiOS/GRDBDemoiOSTests/AppDatabaseTests.swift +++ b/Documentation/DemoApps/GRDBDemoiOS/GRDBDemoiOSTests/AppDatabaseTests.swift @@ -143,64 +143,4 @@ class AppDatabaseTests: XCTestCase { let players = try dbQueue.read(Player.fetchAll) XCTAssertEqual(players, [player]) } - - func test_observePlayersOrderedByName() throws { - // Given a players database that contains two players - let dbQueue = DatabaseQueue() - let appDatabase = try AppDatabase(dbQueue) - var player1 = Player(id: nil, name: "Arthur", score: 100) - var player2 = Player(id: nil, name: "Barbara", score: 1000) - try dbQueue.write { db in - try player1.insert(db) - try player2.insert(db) - } - - // When we observe players and wait for the first value - let exp = expectation(description: "Players") - var players: [Player]? - let cancellable = appDatabase.observePlayersOrderedByName( - onError: { error in - XCTFail("Unexpected error \(error)") - }, - onChange: { - players = $0 - exp.fulfill() - }) - withExtendedLifetime(cancellable) { - waitForExpectations(timeout: 1, handler: nil) - } - - // Then the players are the two players ordered by name - XCTAssertEqual(players, [player1, player2]) - } - - func test_observePlayersOrderedByScore() throws { - // Given a players database that contains two players - let dbQueue = DatabaseQueue() - let appDatabase = try AppDatabase(dbQueue) - var player1 = Player(id: nil, name: "Arthur", score: 100) - var player2 = Player(id: nil, name: "Barbara", score: 1000) - try dbQueue.write { db in - try player1.insert(db) - try player2.insert(db) - } - - // When we observe players and wait for the first value - let exp = expectation(description: "Players") - var players: [Player]? - let cancellable = appDatabase.observePlayersOrderedByScore( - onError: { error in - XCTFail("Unexpected error \(error)") - }, - onChange: { - players = $0 - exp.fulfill() - }) - withExtendedLifetime(cancellable) { - waitForExpectations(timeout: 1, handler: nil) - } - - // Then the players are the two players ordered by score descending - XCTAssertEqual(players, [player2, player1]) - } }