From ab7970d470f4ddede0a4bbc4496dfc57b8fcf568 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Thu, 1 Nov 2018 22:58:20 +0100 Subject: [PATCH] Documentation --- README.md | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index f3af492d5..829450354 100644 --- a/README.md +++ b/README.md @@ -6343,10 +6343,11 @@ let observer = observation.start(in: dbQueue) { teamInfo: TeamInfo? in } ``` - -The fetch closure may return consecutive identical values. You can filter out those duplicates with the `ValueObservation.tracking(_:fetchDistinct:)` method. It requires the fetched value to adopt the Equatable protocol: +It may happen that a database change does not modify the fetched values. In this case, you'll be notified with consecutive identical values. You can filter out those duplicates with the `ValueObservation.tracking(_:fetchDistinct:)` method. It requires the fetched value to adopt the Equatable protocol: ```swift +// When the `player` table is changed, fetch the total number of players, and +// the ten best ones: struct HallOfFame: Equatable { var count: Int var players: [Player] @@ -6354,7 +6355,7 @@ struct HallOfFame: Equatable { let observation = ValueObservation.tracking( Player.all(), - fetchDistinct: { db -> TeamInfo? in + fetchDistinct: { db -> HallOfFame in let count = try Player.fetchCount(db) let players = try Player .order(Column("score").desc) @@ -6368,7 +6369,7 @@ let observer = observation.start(in: dbQueue) { hallOfFame: HallOfFame in } ``` -The initial parameter of the `ValueObservation.tracking(_:fetch:)` method can be fed with requests, and generally speaking, values that adopt the **DatabaseRegionConvertible** protocol. +The initial parameter of the `ValueObservation.tracking(_:fetch:)` and `ValueObservation.tracking(_:fetchDistinct:)` methods can be fed with requests, and generally speaking, values that adopt the **DatabaseRegionConvertible** protocol. Use this protocol when you want to encapsulate your complex requests in a dedicated type. Our example above can be rewritten as below: