From ec0e8672beaba533d3a25c3f4c6bda4826993ac8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sun, 14 Jun 2020 11:03:24 +0200 Subject: [PATCH] Documentation, cleanup --- GRDB/Core/DatabasePool.swift | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/GRDB/Core/DatabasePool.swift b/GRDB/Core/DatabasePool.swift index 94abe37b2..9896121cc 100644 --- a/GRDB/Core/DatabasePool.swift +++ b/GRDB/Core/DatabasePool.swift @@ -947,14 +947,23 @@ extension DatabasePool: DatabaseReader { do { // Transaction is needed for version snapshotting try db.inTransaction(.deferred) { - var fetchNeeded = true - withExtendedLifetime(initialSnapshot) { - if let initialVersion = initialSnapshot.version { - do { - fetchNeeded = try db.wasChanged(since: initialVersion) - } catch { - // ignore: we'll just re-fetch - } + // Keep DatabaseSnaphot alive until we have compared + // database versions. It prevents database checkpointing, + // and keeps versions (`sqlite3_snapshot`) valid + // and comparable. + let fetchNeeded: Bool = withExtendedLifetime(initialSnapshot) { + // Version is nil if SQLite is not compiled with + // SQLITE_ENABLE_SNAPSHOT, or if the DatabaseSnaphot + // could not grab its version. In this case, we don't + // care, and just fetch a fresh value. + guard let initialVersion = initialSnapshot.version else { + return true + } + do { + return try db.wasChanged(since: initialVersion) + } catch { + // ignore: we'll just re-fetch + return true } }