mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-24 04:07:47 +00:00
SE-0345: if let x { }
https://github.com/apple/swift-evolution/blob/main/proposals/0345-if-let-shorthand.md
This commit is contained in:
1 parent
58293befc9
commit
dd55175978
24 files changed
+84
-99
No files matched your search
+1
-1
@@ -144,7 +144,7 @@ class PlayerListViewController: UITableViewController {
|
||||
scheduling: .immediate,
|
||||
onError: { error in fatalError("Unexpected error: \(error)") },
|
||||
onChange: { [weak self] players in
|
||||
guard let self = self else { return }
|
||||
guard let self else { return }
|
||||
self.configureTitle(from: players)
|
||||
self.configureDataSource(from: players)
|
||||
})
|
||||
|
||||
@@ -333,7 +333,7 @@ public class SQLStatementCursor: Cursor {
|
||||
}
|
||||
|
||||
var statementEnd: UnsafePointer<Int8>? = nil
|
||||
let compiledStatement = try Statement(
|
||||
let statement = try Statement(
|
||||
database: database,
|
||||
statementStart: baseAddress + offset,
|
||||
statementEnd: &statementEnd,
|
||||
@@ -341,7 +341,7 @@ public class SQLStatementCursor: Cursor {
|
||||
|
||||
offset = statementEnd! - baseAddress
|
||||
|
||||
guard let statement = compiledStatement else {
|
||||
guard let statement else {
|
||||
try checkArgumentsAreEmpty()
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -373,7 +373,7 @@ public final class Database: CustomStringConvertible, CustomDebugStringConvertib
|
||||
func close() throws {
|
||||
SchedulingWatchdog.preconditionValidQueue(self)
|
||||
|
||||
guard let sqliteConnection = sqliteConnection else {
|
||||
guard let sqliteConnection else {
|
||||
// Already closed
|
||||
return
|
||||
}
|
||||
@@ -413,7 +413,7 @@ public final class Database: CustomStringConvertible, CustomDebugStringConvertib
|
||||
func close_v2() {
|
||||
SchedulingWatchdog.preconditionValidQueue(self)
|
||||
|
||||
guard let sqliteConnection = sqliteConnection else {
|
||||
guard let sqliteConnection else {
|
||||
// Already closed
|
||||
return
|
||||
}
|
||||
@@ -643,7 +643,7 @@ public final class Database: CustomStringConvertible, CustomDebugStringConvertib
|
||||
_ x: UnsafeMutableRawPointer?,
|
||||
_ sqlite3_expanded_sql: @escaping @convention(c) (OpaquePointer?) -> UnsafeMutablePointer<Int8>?)
|
||||
{
|
||||
guard let trace = trace else { return }
|
||||
guard let trace else { return }
|
||||
|
||||
switch mask {
|
||||
case SQLITE_TRACE_STMT:
|
||||
|
||||
@@ -348,7 +348,7 @@ extension DatabasePool: DatabaseReader {
|
||||
@_disfavoredOverload // SR-15150 Async overloading in protocol implementation fails
|
||||
public func read<T>(_ value: (Database) throws -> T) throws -> T {
|
||||
GRDBPrecondition(currentReader == nil, "Database methods are not reentrant.")
|
||||
guard let readerPool = readerPool else {
|
||||
guard let readerPool else {
|
||||
throw DatabaseError.connectionIsClosed()
|
||||
}
|
||||
return try readerPool.get { reader in
|
||||
@@ -362,7 +362,7 @@ extension DatabasePool: DatabaseReader {
|
||||
}
|
||||
|
||||
public func asyncRead(_ value: @escaping (Result<Database, Error>) -> Void) {
|
||||
guard let readerPool = self.readerPool else {
|
||||
guard let readerPool else {
|
||||
value(.failure(DatabaseError(resultCode: .SQLITE_MISUSE, message: "Connection is closed")))
|
||||
return
|
||||
}
|
||||
@@ -394,7 +394,7 @@ extension DatabasePool: DatabaseReader {
|
||||
@_disfavoredOverload // SR-15150 Async overloading in protocol implementation fails
|
||||
public func unsafeRead<T>(_ value: (Database) throws -> T) throws -> T {
|
||||
GRDBPrecondition(currentReader == nil, "Database methods are not reentrant.")
|
||||
guard let readerPool = readerPool else {
|
||||
guard let readerPool else {
|
||||
throw DatabaseError.connectionIsClosed()
|
||||
}
|
||||
return try readerPool.get { reader in
|
||||
@@ -406,7 +406,7 @@ extension DatabasePool: DatabaseReader {
|
||||
}
|
||||
|
||||
public func asyncUnsafeRead(_ value: @escaping (Result<Database, Error>) -> Void) {
|
||||
guard let readerPool = self.readerPool else {
|
||||
guard let readerPool else {
|
||||
value(.failure(DatabaseError(resultCode: .SQLITE_MISUSE, message: "Connection is closed")))
|
||||
return
|
||||
}
|
||||
@@ -437,7 +437,7 @@ extension DatabasePool: DatabaseReader {
|
||||
if let reader = currentReader {
|
||||
return try reader.reentrantSync(value)
|
||||
} else {
|
||||
guard let readerPool = readerPool else {
|
||||
guard let readerPool else {
|
||||
throw DatabaseError.connectionIsClosed()
|
||||
}
|
||||
return try readerPool.get { reader in
|
||||
@@ -527,7 +527,7 @@ extension DatabasePool: DatabaseReader {
|
||||
let isolationSemaphore = DispatchSemaphore(value: 0)
|
||||
|
||||
do {
|
||||
guard let readerPool = readerPool else {
|
||||
guard let readerPool else {
|
||||
throw DatabaseError.connectionIsClosed()
|
||||
}
|
||||
let (reader, releaseReader) = try readerPool.get()
|
||||
@@ -620,7 +620,7 @@ extension DatabasePool: DatabaseReader {
|
||||
/// Returns a reader that can be used from the current dispatch queue,
|
||||
/// if any.
|
||||
private var currentReader: SerializedDatabase? {
|
||||
guard let readerPool = readerPool else {
|
||||
guard let readerPool else {
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -652,7 +652,7 @@ extension DatabasePool: DatabaseReader {
|
||||
|
||||
@_disfavoredOverload // SR-15150 Async overloading in protocol implementation fails
|
||||
public func barrierWriteWithoutTransaction<T>(_ updates: (Database) throws -> T) throws -> T {
|
||||
guard let readerPool = readerPool else {
|
||||
guard let readerPool else {
|
||||
throw DatabaseError.connectionIsClosed()
|
||||
}
|
||||
return try readerPool.barrier {
|
||||
@@ -661,7 +661,7 @@ extension DatabasePool: DatabaseReader {
|
||||
}
|
||||
|
||||
public func asyncBarrierWriteWithoutTransaction(_ updates: @escaping (Result<Database, Error>) -> Void) {
|
||||
guard let readerPool = readerPool else {
|
||||
guard let readerPool else {
|
||||
updates(.failure(DatabaseError.connectionIsClosed()))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -38,7 +38,7 @@ public struct DatabaseRegion: CustomStringConvertible, Equatable {
|
||||
|
||||
/// Returns whether the region is empty.
|
||||
public var isEmpty: Bool {
|
||||
guard let tableRegions = tableRegions else {
|
||||
guard let tableRegions else {
|
||||
// full database
|
||||
return false
|
||||
}
|
||||
@@ -87,7 +87,7 @@ public struct DatabaseRegion: CustomStringConvertible, Equatable {
|
||||
/// this intersection. It is currently only used as support for
|
||||
/// the isModified(byEventsOfKind:) method.
|
||||
func intersection(_ other: DatabaseRegion) -> DatabaseRegion {
|
||||
guard let tableRegions = tableRegions else { return other }
|
||||
guard let tableRegions else { return other }
|
||||
guard let otherTableRegions = other.tableRegions else { return self }
|
||||
|
||||
var tableRegionsIntersection: [CaseInsensitiveIdentifier: TableRegion] = [:]
|
||||
@@ -105,7 +105,7 @@ public struct DatabaseRegion: CustomStringConvertible, Equatable {
|
||||
|
||||
/// Only keeps those rowIds in the given table
|
||||
func tableIntersection(_ table: String, rowIds: Set<Int64>) -> DatabaseRegion {
|
||||
guard var tableRegions = tableRegions else {
|
||||
guard var tableRegions else {
|
||||
return DatabaseRegion(table: table, rowIds: rowIds)
|
||||
}
|
||||
|
||||
@@ -125,7 +125,7 @@ public struct DatabaseRegion: CustomStringConvertible, Equatable {
|
||||
|
||||
/// Returns the union of this region and the given one.
|
||||
public func union(_ other: DatabaseRegion) -> DatabaseRegion {
|
||||
guard let tableRegions = tableRegions else { return .fullDatabase }
|
||||
guard let tableRegions else { return .fullDatabase }
|
||||
guard let otherTableRegions = other.tableRegions else { return .fullDatabase }
|
||||
|
||||
var tableRegionsUnion: [CaseInsensitiveIdentifier: TableRegion] = [:]
|
||||
@@ -170,7 +170,7 @@ public struct DatabaseRegion: CustomStringConvertible, Equatable {
|
||||
/// This method removes views (assuming no table exists with the same name
|
||||
/// as a view).
|
||||
private func canonicalTables(_ db: Database) throws -> DatabaseRegion {
|
||||
guard let tableRegions = tableRegions else { return .fullDatabase }
|
||||
guard let tableRegions else { return .fullDatabase }
|
||||
var region = DatabaseRegion()
|
||||
for (table, tableRegion) in tableRegions {
|
||||
if let canonicalTableName = try db.canonicalTableName(table.rawValue) {
|
||||
@@ -183,7 +183,7 @@ public struct DatabaseRegion: CustomStringConvertible, Equatable {
|
||||
|
||||
/// Returns a region which doesn't contain any SQLite internal table.
|
||||
private func ignoringInternalSQLiteTables() -> DatabaseRegion {
|
||||
guard let tableRegions = tableRegions else { return .fullDatabase }
|
||||
guard let tableRegions else { return .fullDatabase }
|
||||
let filteredRegions = tableRegions.filter {
|
||||
!Database.isSQLiteInternalTable($0.key.rawValue)
|
||||
}
|
||||
@@ -207,7 +207,7 @@ extension DatabaseRegion {
|
||||
/// in the TransactionObserver.observes(eventsOfKind:) method, by calling
|
||||
/// region.isModified(byEventsOfKind:)
|
||||
public func isModified(by event: DatabaseEvent) -> Bool {
|
||||
guard let tableRegions = tableRegions else {
|
||||
guard let tableRegions else {
|
||||
// Full database: all changes are impactful
|
||||
return true
|
||||
}
|
||||
@@ -253,7 +253,7 @@ extension DatabaseRegion {
|
||||
extension DatabaseRegion {
|
||||
/// :nodoc:
|
||||
public var description: String {
|
||||
guard let tableRegions = tableRegions else {
|
||||
guard let tableRegions else {
|
||||
return "full database"
|
||||
}
|
||||
if tableRegions.isEmpty {
|
||||
@@ -328,7 +328,7 @@ private struct TableRegion: Equatable {
|
||||
}
|
||||
|
||||
func contains(rowID: Int64) -> Bool {
|
||||
guard let rowIds = rowIds else {
|
||||
guard let rowIds else {
|
||||
return true
|
||||
}
|
||||
return rowIds.contains(rowID)
|
||||
|
||||
@@ -458,7 +458,7 @@ extension Row {
|
||||
|
||||
/// Returns self if adapter is nil
|
||||
func adapted(with adapter: RowAdapter?, layout: _RowLayout) throws -> Row {
|
||||
guard let adapter = adapter else {
|
||||
guard let adapter else {
|
||||
return self
|
||||
}
|
||||
return try Row(base: self, adapter: adapter._layoutedAdapter(from: layout))
|
||||
|
||||
@@ -115,12 +115,12 @@ public final class Statement {
|
||||
sql: String(cString: statementStart))
|
||||
}
|
||||
|
||||
guard let statement = sqliteStatement else {
|
||||
guard let sqliteStatement else {
|
||||
return nil
|
||||
}
|
||||
|
||||
self.database = database
|
||||
self.sqliteStatement = statement
|
||||
self.sqliteStatement = sqliteStatement
|
||||
self.databaseRegion = authorizer.selectedRegion
|
||||
self.invalidatesDatabaseSchemaCache = authorizer.invalidatesDatabaseSchemaCache
|
||||
self.transactionEffect = authorizer.transactionEffect
|
||||
@@ -467,7 +467,7 @@ final class StatementCursor: DatabaseCursor {
|
||||
|
||||
extension Statement {
|
||||
var releasesDatabaseLock: Bool {
|
||||
guard let transactionEffect = transactionEffect else {
|
||||
guard let transactionEffect else {
|
||||
return false
|
||||
}
|
||||
|
||||
|
||||
@@ -76,12 +76,12 @@ public struct DatabaseDateComponents: DatabaseValueConvertible, StatementColumnC
|
||||
return nil
|
||||
}
|
||||
let length = Int(sqlite3_column_bytes(sqliteStatement, index)) // avoid an strlen
|
||||
let optionalComponents = cString.withMemoryRebound(
|
||||
let components = cString.withMemoryRebound(
|
||||
to: Int8.self,
|
||||
capacity: length + 1 /* trailing \0 */) { cString in
|
||||
SQLiteDateParser().components(cString: cString, length: length)
|
||||
}
|
||||
guard let components = optionalComponents else {
|
||||
guard let components else {
|
||||
return nil
|
||||
}
|
||||
self.init(components.dateComponents, format: components.format)
|
||||
|
||||
@@ -38,10 +38,10 @@ final class WALSnapshot {
|
||||
let code: CInt = withUnsafeMutablePointer(to: &snapshot) {
|
||||
return sqlite3_snapshot_get(db.sqliteConnection, "main", $0)
|
||||
}
|
||||
guard code == SQLITE_OK, let s = snapshot else {
|
||||
guard code == SQLITE_OK, let snapshot else {
|
||||
return nil
|
||||
}
|
||||
self.snapshot = s
|
||||
self.snapshot = snapshot
|
||||
}
|
||||
|
||||
deinit {
|
||||
|
||||
+2
-2
@@ -259,10 +259,10 @@ public struct FTS5: VirtualTableModule {
|
||||
_ = sqlite3_bind_pointer(statement, 1, &api, typePointer, nil)
|
||||
}
|
||||
sqlite3_step(statement)
|
||||
guard let result = api else {
|
||||
guard let api else {
|
||||
fatalError("FTS5 is not available")
|
||||
}
|
||||
return result
|
||||
return api
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -68,7 +68,7 @@ extension Database {
|
||||
let constructor = FTS5TokenizerConstructor(
|
||||
db: self,
|
||||
constructor: { (db, arguments, tokenizerHandle) in
|
||||
guard let tokenizerHandle = tokenizerHandle else {
|
||||
guard let tokenizerHandle else {
|
||||
return SQLITE_ERROR
|
||||
}
|
||||
do {
|
||||
@@ -92,7 +92,7 @@ extension Database {
|
||||
let constructorPointer = Unmanaged.passRetained(constructor).toOpaque()
|
||||
|
||||
func deleteConstructor(constructorPointer: UnsafeMutableRawPointer?) {
|
||||
guard let constructorPointer = constructorPointer else { return }
|
||||
guard let constructorPointer else { return }
|
||||
Unmanaged<AnyObject>.fromOpaque(constructorPointer).release()
|
||||
}
|
||||
|
||||
@@ -103,7 +103,7 @@ extension Database {
|
||||
tokenizerHandle: UnsafeMutablePointer<OpaquePointer?>?)
|
||||
-> Int32
|
||||
{
|
||||
guard let constructorPointer = constructorPointer else {
|
||||
guard let constructorPointer else {
|
||||
return SQLITE_ERROR
|
||||
}
|
||||
let constructor = Unmanaged<FTS5TokenizerConstructor>.fromOpaque(constructorPointer).takeUnretainedValue()
|
||||
@@ -119,7 +119,7 @@ extension Database {
|
||||
}
|
||||
|
||||
func xDeleteTokenizer(tokenizerPointer: OpaquePointer?) {
|
||||
guard let tokenizerPointer = tokenizerPointer else { return }
|
||||
guard let tokenizerPointer else { return }
|
||||
Unmanaged<AnyObject>.fromOpaque(UnsafeMutableRawPointer(tokenizerPointer)).release()
|
||||
}
|
||||
|
||||
@@ -133,7 +133,7 @@ extension Database {
|
||||
tokenCallback: (@convention(c) (UnsafeMutableRawPointer?, Int32, UnsafePointer<Int8>?, Int32, Int32, Int32) -> Int32)?)
|
||||
-> Int32
|
||||
{
|
||||
guard let tokenizerPointer = tokenizerPointer else {
|
||||
guard let tokenizerPointer else {
|
||||
return SQLITE_ERROR
|
||||
}
|
||||
let object = Unmanaged<AnyObject>
|
||||
|
||||
@@ -128,7 +128,7 @@ extension FTS5Tokenizer {
|
||||
pText: pText,
|
||||
nText: nText,
|
||||
tokenCallback: { (contextPointer, flags, pToken, nToken, _ /* iStart */, _ /* iEnd */) -> Int32 in
|
||||
guard let contextPointer = contextPointer else {
|
||||
guard let contextPointer else {
|
||||
return SQLITE_ERROR
|
||||
}
|
||||
|
||||
|
||||
@@ -12,7 +12,7 @@ extension TableRequest where Self: FilteredRequest {
|
||||
/// If the search pattern is nil, the request does not match any
|
||||
/// database row.
|
||||
public func matching(_ pattern: FTS3Pattern?) -> Self {
|
||||
guard let pattern = pattern else {
|
||||
guard let pattern else {
|
||||
return none()
|
||||
}
|
||||
let alias = TableAlias()
|
||||
|
||||
@@ -17,7 +17,7 @@ extension TableRequest where Self: FilteredRequest {
|
||||
/// all requests by the `TableRecord.databaseSelection` property, or
|
||||
/// for individual requests with the `TableRecord.select` method.
|
||||
public func matching(_ pattern: FTS5Pattern?) -> Self {
|
||||
guard let pattern = pattern else {
|
||||
guard let pattern else {
|
||||
return none()
|
||||
}
|
||||
let alias = TableAlias()
|
||||
|
||||
@@ -306,7 +306,7 @@ extension TableRequest where Self: FilteredRequest, Self: TypedRequest {
|
||||
///
|
||||
/// - parameter key: A unique key
|
||||
public func filter(key: [String: DatabaseValueConvertible?]?) -> Self {
|
||||
guard let key = key else {
|
||||
guard let key else {
|
||||
return none()
|
||||
}
|
||||
return filter(keys: [key])
|
||||
|
||||
@@ -150,7 +150,7 @@ private struct _RowDecoder<R: FetchableRecord>: Decoder {
|
||||
// swiftlint:enable comma
|
||||
|
||||
private func decodeColumn(forKey key: Key) throws -> String {
|
||||
guard let _columnForKey = _columnForKey else {
|
||||
guard let _columnForKey else {
|
||||
return key.stringValue
|
||||
}
|
||||
|
||||
|
||||
@@ -315,7 +315,7 @@ extension FetchableRecord where Self: TableRecord {
|
||||
/// - returns: An optional record.
|
||||
/// - throws: A DatabaseError is thrown whenever an SQLite error occurs.
|
||||
public static func fetchOne(_ db: Database, key: [String: DatabaseValueConvertible?]?) throws -> Self? {
|
||||
guard let key = key else {
|
||||
guard let key else {
|
||||
// Avoid hitting the database
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ import Foundation
|
||||
extension String {
|
||||
/// "player" -> "Player"
|
||||
var uppercasingFirstCharacter: String {
|
||||
guard let first = first else {
|
||||
guard let first else {
|
||||
return self
|
||||
}
|
||||
return String(first).uppercased() + dropFirst()
|
||||
|
||||
@@ -192,11 +192,10 @@ final class ValueConcurrentObserver<Reducer: ValueReducer> {
|
||||
extension ValueConcurrentObserver {
|
||||
// Starts the observation
|
||||
func start() -> DatabaseCancellable {
|
||||
// TODO: [SR-214] remove -Opt suffix when we only support Xcode 12.5.1+
|
||||
let (notificationCallbacksOpt, databaseAccessOpt) = lock.synchronized {
|
||||
let (notificationCallbacks, databaseAccess) = lock.synchronized {
|
||||
(self.notificationCallbacks, self.databaseAccess)
|
||||
}
|
||||
guard let notificationCallbacks = notificationCallbacksOpt, let databaseAccess = databaseAccessOpt else {
|
||||
guard let notificationCallbacks, let databaseAccess else {
|
||||
// Likely a GRDB bug: during a synchronous start, user is not
|
||||
// able to cancel observation.
|
||||
fatalError("can't start a cancelled or failed observation")
|
||||
@@ -351,9 +350,8 @@ extension ValueConcurrentObserver {
|
||||
|
||||
// Notify
|
||||
self.scheduler.schedule {
|
||||
// TODO: [SR-214] remove -Opt suffix when we only support Xcode 12.5.1+
|
||||
let onChangeOpt = self.lock.synchronized { self.notificationCallbacks?.onChange }
|
||||
guard let onChange = onChangeOpt else { return /* Cancelled */ }
|
||||
let onChange = self.lock.synchronized { self.notificationCallbacks?.onChange }
|
||||
guard let onChange else { return /* Cancelled */ }
|
||||
onChange(initialValue)
|
||||
}
|
||||
}
|
||||
@@ -380,9 +378,8 @@ extension ValueConcurrentObserver {
|
||||
initialRegion: DatabaseRegion)
|
||||
{
|
||||
databaseAccess.dbPool.asyncWriteWithoutTransaction { writerDB in
|
||||
// TODO: [SR-214] remove -Opt suffix when we only support Xcode 12.5.1+
|
||||
let eventsOpt = self.lock.synchronized { self.notificationCallbacks?.events }
|
||||
guard let events = eventsOpt else { return /* Cancelled */ }
|
||||
let events = self.lock.synchronized { self.notificationCallbacks?.events }
|
||||
guard let events else { return /* Cancelled */ }
|
||||
|
||||
do {
|
||||
var observedRegion = initialRegion
|
||||
@@ -394,7 +391,7 @@ extension ValueConcurrentObserver {
|
||||
// and keeps WAL snapshots (`sqlite3_snapshot`) valid
|
||||
// and comparable.
|
||||
let isModified: Bool = withExtendedLifetime(initialSnapshot) {
|
||||
guard let initialWALSnapshot = initialWALSnapshot,
|
||||
guard let initialWALSnapshot,
|
||||
let currentWALSnapshot = WALSnapshot(writerDB)
|
||||
else {
|
||||
return true
|
||||
@@ -443,9 +440,8 @@ extension ValueConcurrentObserver {
|
||||
// Notify
|
||||
if let value = value {
|
||||
self.scheduler.schedule {
|
||||
// TODO: [SR-214] remove -Opt suffix when we only support Xcode 12.5.1+
|
||||
let onChangeOpt = self.lock.synchronized { self.notificationCallbacks?.onChange }
|
||||
guard let onChange = onChangeOpt else { return /* Cancelled */ }
|
||||
let onChange = self.lock.synchronized { self.notificationCallbacks?.onChange }
|
||||
guard let onChange else { return /* Cancelled */ }
|
||||
onChange(value)
|
||||
}
|
||||
}
|
||||
@@ -489,13 +485,12 @@ extension ValueConcurrentObserver: TransactionObserver {
|
||||
// Reset the isModified flag until next transaction
|
||||
observationState.isModified = false
|
||||
|
||||
// TODO: [SR-214] remove -Opt suffix when we only support Xcode 12.5.1+
|
||||
// Ignore transaction unless we are still notifying database events, and
|
||||
// we can still access the database.
|
||||
let (eventsOpt, databaseAccessOpt) = lock.synchronized {
|
||||
let (events, databaseAccess) = lock.synchronized {
|
||||
(notificationCallbacks?.events, self.databaseAccess)
|
||||
}
|
||||
guard let events = eventsOpt, let databaseAccess = databaseAccessOpt else { return /* Cancelled */ }
|
||||
guard let events, let databaseAccess else { return /* Cancelled */ }
|
||||
|
||||
events.databaseDidChange?()
|
||||
|
||||
@@ -567,9 +562,8 @@ extension ValueConcurrentObserver: TransactionObserver {
|
||||
// Notify value
|
||||
if let value = value {
|
||||
self.scheduler.schedule {
|
||||
// TODO: [SR-214] remove -Opt suffix when we only support Xcode 12.5.1+
|
||||
let onChangeOpt = self.lock.synchronized { self.notificationCallbacks?.onChange }
|
||||
guard let onChange = onChangeOpt else { return /* Cancelled */ }
|
||||
let onChange = self.lock.synchronized { self.notificationCallbacks?.onChange }
|
||||
guard let onChange else { return /* Cancelled */ }
|
||||
onChange(value)
|
||||
}
|
||||
}
|
||||
@@ -593,21 +587,20 @@ extension ValueConcurrentObserver: TransactionObserver {
|
||||
|
||||
extension ValueConcurrentObserver: DatabaseCancellable {
|
||||
func cancel() {
|
||||
// TODO: [SR-214] remove -Opt suffix when we only support Xcode 12.5.1+
|
||||
// Notify cancellation
|
||||
let (eventsOpt, dbPoolOpt): (ValueObservationEvents?, DatabasePool?) = lock.synchronized {
|
||||
let (events, dbPool): (ValueObservationEvents?, DatabasePool?) = lock.synchronized {
|
||||
let events = notificationCallbacks?.events
|
||||
notificationCallbacks = nil
|
||||
return (events, databaseAccess?.dbPool)
|
||||
}
|
||||
|
||||
guard let events = eventsOpt else { return /* Cancelled or failed */ }
|
||||
guard let events else { return /* Cancelled or failed */ }
|
||||
events.didCancel?()
|
||||
|
||||
// Stop observing the database
|
||||
// Do it asynchronously, so that we do not block the current thread:
|
||||
// cancellation may be triggered while a long write access is executing.
|
||||
guard let dbPool = dbPoolOpt else { return /* Failed */ }
|
||||
guard let dbPool else { return /* Failed */ }
|
||||
dbPool.asyncWriteWithoutTransaction { db in
|
||||
self.stopDatabaseObservation(db)
|
||||
}
|
||||
@@ -615,13 +608,12 @@ extension ValueConcurrentObserver: DatabaseCancellable {
|
||||
|
||||
func notifyError(_ error: Error) {
|
||||
scheduler.schedule {
|
||||
// TODO: [SR-214] remove -Opt suffix when we only support Xcode 12.5.1+
|
||||
let eventsOpt: ValueObservationEvents? = self.lock.synchronized {
|
||||
let events: ValueObservationEvents? = self.lock.synchronized {
|
||||
let events = self.notificationCallbacks?.events
|
||||
self.notificationCallbacks = nil
|
||||
return events
|
||||
}
|
||||
guard let events = eventsOpt else { return /* Cancelled */ }
|
||||
guard let events else { return /* Cancelled */ }
|
||||
events.didFail?(error)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -152,11 +152,10 @@ final class ValueWriteOnlyObserver<Writer: DatabaseWriter, Reducer: ValueReducer
|
||||
extension ValueWriteOnlyObserver {
|
||||
// Starts the observation
|
||||
func start() -> DatabaseCancellable {
|
||||
// TODO: [SR-214] remove -Opt suffix when we only support Xcode 12.5.1+
|
||||
let (notificationCallbacksOpt, databaseAccessOpt) = lock.synchronized {
|
||||
(self.notificationCallbacks, self.databaseAccess)
|
||||
let (notificationCallbacks, writer) = lock.synchronized {
|
||||
(self.notificationCallbacks, self.databaseAccess?.writer)
|
||||
}
|
||||
guard let notificationCallbacks = notificationCallbacksOpt, let writer = databaseAccessOpt?.writer else {
|
||||
guard let notificationCallbacks, let writer else {
|
||||
// Likely a GRDB bug: during a synchronous start, user is not
|
||||
// able to cancel observation.
|
||||
fatalError("can't start a cancelled or failed observation")
|
||||
@@ -248,9 +247,8 @@ extension ValueWriteOnlyObserver {
|
||||
|
||||
// Notify
|
||||
self.scheduler.schedule {
|
||||
// TODO: [SR-214] remove -Opt suffix when we only support Xcode 12.5.1+
|
||||
let onChangeOpt = self.lock.synchronized { self.notificationCallbacks?.onChange }
|
||||
guard let onChange = onChangeOpt else { return /* Cancelled */ }
|
||||
let onChange = self.lock.synchronized { self.notificationCallbacks?.onChange }
|
||||
guard let onChange else { return /* Cancelled */ }
|
||||
onChange(initialValue)
|
||||
}
|
||||
}
|
||||
@@ -270,11 +268,10 @@ extension ValueWriteOnlyObserver {
|
||||
/// single database access, we are sure that no concurrent write can happen
|
||||
/// during the initial fetch, and that we won't miss any future change.
|
||||
private func fetchAndStartObservation(_ db: Database) throws -> Reducer.Fetched? {
|
||||
// TODO: [SR-214] remove -Opt suffix when we only support Xcode 12.5.1+
|
||||
let (eventsOpt, fetchOpt) = lock.synchronized {
|
||||
let (events, fetch) = lock.synchronized {
|
||||
(notificationCallbacks?.events, databaseAccess?.fetch)
|
||||
}
|
||||
guard let events = eventsOpt, let fetch = fetchOpt else {
|
||||
guard let events, let fetch else {
|
||||
return nil /* Cancelled */
|
||||
}
|
||||
|
||||
@@ -338,13 +335,12 @@ extension ValueWriteOnlyObserver: TransactionObserver {
|
||||
// Reset the isModified flag until next transaction
|
||||
observationState.isModified = false
|
||||
|
||||
// TODO: [SR-214] remove -Opt suffix when we only support Xcode 12.5.1+
|
||||
// Ignore transaction unless we are still notifying database events, and
|
||||
// we can still fetch fresh values.
|
||||
let (eventsOpt, fetchOpt) = lock.synchronized {
|
||||
let (events, fetch) = lock.synchronized {
|
||||
(notificationCallbacks?.events, databaseAccess?.fetch)
|
||||
}
|
||||
guard let events = eventsOpt, let fetch = fetchOpt else { return /* Cancelled */ }
|
||||
guard let events, let fetch else { return /* Cancelled */ }
|
||||
|
||||
// Notify
|
||||
events.databaseDidChange?()
|
||||
@@ -394,9 +390,8 @@ extension ValueWriteOnlyObserver: TransactionObserver {
|
||||
// Notify value
|
||||
if let value = value {
|
||||
self.scheduler.schedule {
|
||||
// TODO: [SR-214] remove -Opt suffix when we only support Xcode 12.5.1+
|
||||
let onChangeOpt = self.lock.synchronized { self.notificationCallbacks?.onChange }
|
||||
guard let onChange = onChangeOpt else { return /* Cancelled */ }
|
||||
let onChange = self.lock.synchronized { self.notificationCallbacks?.onChange }
|
||||
guard let onChange else { return /* Cancelled */ }
|
||||
onChange(value)
|
||||
}
|
||||
}
|
||||
@@ -417,21 +412,20 @@ extension ValueWriteOnlyObserver: TransactionObserver {
|
||||
|
||||
extension ValueWriteOnlyObserver: DatabaseCancellable {
|
||||
func cancel() {
|
||||
// TODO: [SR-214] remove -Opt suffix when we only support Xcode 12.5.1+
|
||||
// Notify cancellation
|
||||
let (eventsOpt, writerOpt): (ValueObservationEvents?, Writer?) = lock.synchronized {
|
||||
let (events, writer): (ValueObservationEvents?, Writer?) = lock.synchronized {
|
||||
let events = notificationCallbacks?.events
|
||||
notificationCallbacks = nil
|
||||
return (events, databaseAccess?.writer)
|
||||
}
|
||||
|
||||
guard let events = eventsOpt else { return /* Cancelled or failed */ }
|
||||
guard let events else { return /* Cancelled or failed */ }
|
||||
events.didCancel?()
|
||||
|
||||
// Stop observing the database
|
||||
// Do it asynchronously, so that we do not block the current thread:
|
||||
// cancellation may be triggered while a long write access is executing.
|
||||
guard let writer = writerOpt else { return /* Failed */ }
|
||||
guard let writer else { return /* Failed */ }
|
||||
writer.asyncWriteWithoutTransaction { db in
|
||||
self.stopDatabaseObservation(db)
|
||||
}
|
||||
@@ -439,13 +433,12 @@ extension ValueWriteOnlyObserver: DatabaseCancellable {
|
||||
|
||||
func notifyError(_ error: Error) {
|
||||
scheduler.schedule {
|
||||
// TODO: [SR-214] remove -Opt suffix when we only support Xcode 12.5.1+
|
||||
let eventsOpt: ValueObservationEvents? = self.lock.synchronized {
|
||||
let events: ValueObservationEvents? = self.lock.synchronized {
|
||||
let events = self.notificationCallbacks?.events
|
||||
self.notificationCallbacks = nil
|
||||
return events
|
||||
}
|
||||
guard let events = eventsOpt else { return /* Cancelled */ }
|
||||
guard let events else { return /* Cancelled */ }
|
||||
events.didFail?(error)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6202,7 +6202,7 @@ class PlayersViewController: UIViewController {
|
||||
scheduling: .immediate, // <- immediate scheduler
|
||||
onError: { error in ... },
|
||||
onChange: { [weak self] (players: [Player]) in
|
||||
guard let self = self else { return }
|
||||
guard let self else { return }
|
||||
self.updateView(players)
|
||||
})
|
||||
// <- Here the view has already been updated.
|
||||
|
||||
@@ -62,7 +62,7 @@ extension PublisherExpectations {
|
||||
/// }
|
||||
public func get() throws {
|
||||
try recorder.value { (_, completion, remainingElements, consume) in
|
||||
guard let completion = completion else {
|
||||
guard let completion else {
|
||||
consume(remainingElements.count)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@ struct DatabaseTimestamp: DatabaseValueConvertible {
|
||||
/// Creates a DatabaseTimestamp from an Date.
|
||||
/// The result is nil if and only if *date* is nil.
|
||||
init?(_ date: Date?) {
|
||||
guard let date = date else {
|
||||
guard let date else {
|
||||
return nil
|
||||
}
|
||||
self.date = date
|
||||
|
||||
@@ -41,7 +41,7 @@ class DatabaseValueConversionTests : GRDBTestCase {
|
||||
line: UInt = #line) throws
|
||||
{
|
||||
func stringRepresentation(_ value: T?) -> String {
|
||||
guard let value = value else { return "nil" }
|
||||
guard let value else { return "nil" }
|
||||
return String(reflecting: value)
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user