From bfe037c61de6d5f59c8dbc82db7c7f32cc154fcc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Wed, 8 Apr 2020 18:33:56 +0200 Subject: [PATCH] Update documentation --- Documentation/GRDB5MigrationGuide.md | 36 +++++++++++++++++++++-- README.md | 44 ---------------------------- 2 files changed, 34 insertions(+), 46 deletions(-) diff --git a/Documentation/GRDB5MigrationGuide.md b/Documentation/GRDB5MigrationGuide.md index 0d45f9b15..b82e6eed8 100644 --- a/Documentation/GRDB5MigrationGuide.md +++ b/Documentation/GRDB5MigrationGuide.md @@ -177,9 +177,41 @@ Those changes have been applied identically to [GRDBCombine] and [RxGRDB], so th 3. Some value observations used to automatically remove duplicate values. This is no longer automatic. If your application relies on distinct consecutive values, use the [removeDuplicates] operator. -4. ValueObservation used to have a `compactMap` method. This method has been removed without any replacement. +**ValueObservation features have been removed**. -5. ValueObservation used to let application define custom "reducers" using the ValueReducer protocol. These apis are no longer available. See the [#731](https://github.com/groue/GRDB.swift/pull/731) conversation for a solution towards a replacement. +1. ValueObservation used to have a `compactMap` method. This method has been removed without any replacement. + + If your application uses GRDBCombine or RxGRDB, then use the `compactMap` method from Combine or RxSwift instead. + +2. ValueObservation used to have a `combine` method. This method has been removed without any replacement. + + In your application, replace combined observations with a single observation: + + ```swift + // BEFORE: GRDB 4 + let playerCountObservation = ValueObservation.tracking(Player.fetchCount) + let bestPlayersObservation = ValueObservation.tracking(Player + .limit(10) + .order(Column("score").desc) + .fetchAll) + let observation = ValueObservation + .combine(playerCountObservation, bestPlayersObservation) + .map(HallOfFame.init) + + // NEW: GRDB 5 + let observation = ValueObservation.tracking { db -> HallOfFame in + let playerCount = try Player.fetchCount(db) + let bestPlayers = try Player + .limit(10) + .order(Column("score").desc) + .fetchAll(db) + return HallOfFame(playerCount: playerCount, bestPlayers: bestPlayers) + } + ``` + + As is previous versions of GRDB, do not use the `combineLatest` operators of Combine or RxSwift in order to combine several ValueObservation. You would lose all guarantees of [data consistency](https://en.wikipedia.org/wiki/Consistency_(database_systems)). + +3. ValueObservation used to let application define custom "reducers" using the ValueReducer protocol. These apis are no longer available. See the [#731](https://github.com/groue/GRDB.swift/pull/731) conversation for a solution towards a replacement. ## Other Changes diff --git a/README.md b/README.md index dcea41ca4..fd1bebc08 100644 --- a/README.md +++ b/README.md @@ -5711,7 +5711,6 @@ let cancellable = observation.start( - [ValueObservation.map](#valueobservationmap) - [ValueObservation.removeDuplicates](#valueobservationremoveduplicates) -- [ValueObservation.combine](#valueobservationcombine) - [ValueObservation.requiresWriteAccess](#valueobservationrequireswriteaccess) @@ -5772,49 +5771,6 @@ let observation = ValueObservation ``` -#### ValueObservation.combine - -The `ValueObservation.combine` static method builds a single observation from several (up to eight): - -```swift -// An observation of Int -let playerCountObservation = ValueObservation.tracking { db in - try Player.fetchCount(db) -} -// An observation of [Player] -let bestPlayersObservation = ValueObservation.tracking { db in - try Player - .limit(10) - .order(Column("score").desc) - .fetchAll(db) -} - -// The combined observation of (Int, [Player]) -let observation = ValueObservation.combine( - playerCountObservation, - bestPlayersObservation) -``` - -Combining observations together provides the guarantee that notified values are [**consistent**](https://en.wikipedia.org/wiki/Consistency_(database_systems)). - -`combine` also exists as an instance method: - -```swift -struct HallOfFame { - var totalPlayerCount: Int - var bestPlayers: [Player] -} - -// An observation of HallOfFame -let observation = playerCountObservation.combine(bestPlayersObservation) { - HallOfFame(totalPlayerCount: $0, bestPlayers: $1) -} -``` - - -> :point_up: **Note**: readers who are familiar with Reactive Programming will recognize the [CombineLatest](http://reactivex.io/documentation/operators/combinelatest.html) operator in the ValueObservation `combine` method. This is partially exact, because the reactive operator is unable to guarantee data consistency. If you use a Reactive layer such as [GRDBCombine] or [RxGRDB], make sure you compose observations with `ValueObservation.combine`, and never with the CombineLatest operator. - - #### ValueObservation.requiresWriteAccess The `requiresWriteAccess` property is false by default. When true, a ValueObservation has a write access to the database, and its fetches are automatically wrapped in a [savepoint](#transactions-and-savepoints):