diff --git a/CHANGELOG.md b/CHANGELOG.md index 29caf77a6..cd6c2c397 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -81,6 +81,7 @@ GRDB adheres to [Semantic Versioning](https://semver.org/), with one exception: - **Fixed**: [#980](https://github.com/groue/GRDB.swift/pull/980) by [@jroselightricks](https://github.com/jroselightricks): Fix spelling - **Fixed**: [#989](https://github.com/groue/GRDB.swift/pull/989) by [@pp5x](https://github.com/pp5x): FTS: add support of ifNotExists in synchronize() - **Fixed**: [#999](https://github.com/groue/GRDB.swift/issues/999): `request(for: association)` no longer crashes when the foreign key contains a NULL value. +- **Fixed**: Fix thread unsafety in `ValueObservation.print()` - **New**: The `selectID()` method is now available for [Identifiable Records](README.md#identifiable-records) - **New**: Cached prepared statements can profit from [SQL Interpolation](Documentation/SQLInterpolation.md): diff --git a/GRDB/ValueObservation/ValueObservation.swift b/GRDB/ValueObservation/ValueObservation.swift index b600b379b..86e6f89e5 100644 --- a/GRDB/ValueObservation/ValueObservation.swift +++ b/GRDB/ValueObservation/ValueObservation.swift @@ -172,16 +172,31 @@ extension ValueObservation: Refinable { to stream: TextOutputStream? = nil) -> ValueObservation> { + let lock = NSLock() let prefix = prefix.isEmpty ? "" : "\(prefix): " var stream = stream ?? PrintOutputStream() return handleEvents( - willStart: { stream.write("\(prefix)start") }, - willFetch: { stream.write("\(prefix)fetch") }, - willTrackRegion: { stream.write("\(prefix)tracked region: \($0)") }, - databaseDidChange: { stream.write("\(prefix)database did change") }, - didReceiveValue: { stream.write("\(prefix)value: \($0)") }, - didFail: { stream.write("\(prefix)failure: \($0)") }, - didCancel: { stream.write("\(prefix)cancel") }) + willStart: { + lock.lock(); defer { lock.unlock() } + stream.write("\(prefix)start") }, + willFetch: { + lock.lock(); defer { lock.unlock() } + stream.write("\(prefix)fetch") }, + willTrackRegion: { + lock.lock(); defer { lock.unlock() } + stream.write("\(prefix)tracked region: \($0)") }, + databaseDidChange: { + lock.lock(); defer { lock.unlock() } + stream.write("\(prefix)database did change") }, + didReceiveValue: { + lock.lock(); defer { lock.unlock() } + stream.write("\(prefix)value: \($0)") }, + didFail: { + lock.lock(); defer { lock.unlock() } + stream.write("\(prefix)failure: \($0)") }, + didCancel: { + lock.lock(); defer { lock.unlock() } + stream.write("\(prefix)cancel") }) } // MARK: - Fetching Values