This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// To run this playground, select and build the GRDBOSX scheme. | |
// | |
// This sample code shows how to use GRDB to synchronize a database table | |
// with a JSON payload. We use as few SQL queries as possible: | |
// | |
// - Only one SELECT query. | |
// - One query per insert, delete, and update. | |
// - Useless UPDATE statements are avoided. | |
import Foundation |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// A replication of the convenience fetching methods of FetchableRecord, | |
// built for another record protocol: ContextFetchableRecord | |
protocol ContextFetchableRecord { | |
associatedtype Context | |
init(row: Row, context: Context) | |
} | |
extension ContextFetchableRecord { | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//: # Toward Enum Key Paths: a Protocol Hierarchy for Read-Only Key Paths | |
//: | |
//: This playground is an experiment for a protocol hierarchy of read-only | |
//: key paths that can handle both throwing and non-throwing getters. | |
//: | |
//: Since the Swift language has no support for throwing subscripts, we'll | |
//: perform our experiments with a very simplified setup that involves a single | |
//: getter function. | |
//: | |
//: ## The Protocol Hierarchy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Combine | |
/// A publisher that delivers values to its downstream subscriber on a | |
/// specific scheduler. | |
/// | |
/// Unlike Combine's Publishers.ReceiveOn, ReceiveValuesOn only re-schedule | |
/// values and completion. It does not re-schedule subscription. | |
struct ReceiveValuesOn<Upstream: Publisher, Context: Scheduler>: Publisher { | |
typealias Output = Upstream.Output | |
typealias Failure = Upstream.Failure |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Combine | |
import Foundation | |
/// A thread-safe store for cancellables which addresses usability pain points | |
/// with stock Combine apis. | |
/// | |
/// ## Thread-safe storage of cancellables | |
/// | |
/// let cancelBag = CancelBag() | |
/// cancellable.store(in: cancelBag) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
/// To create an operation: | |
/// | |
/// 1. Subclass AsynchronousOperation, override main, and eventually cancel the | |
/// operation, or set result to a non-nil value. | |
/// | |
/// 2. Use makeOperation { op in ... }, and eventually cancel the | |
/// operation, or set result to a non-nil value. | |
open class AsynchronousOperation<Output, Failure: Error>: Operation { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import GRDB | |
extension QueryInterfaceRequest where RowDecoder: MutablePersistableRecord { | |
/// Deletes all records in the database, but the ones selected by the request. | |
/// | |
/// For example: | |
/// | |
/// // Delete all players whose score is below 1000 | |
/// let bestPlayers = Player.filter(Column("score") > 1000) | |
/// try dbQueue.write(bestPlayers.deleteAllOther) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Combine | |
extension Publisher where Failure == Never { | |
/// Same as assign(to:on:), but root object is not retained. | |
func assignWeakly<Root: AnyObject>( | |
to keyPath: ReferenceWritableKeyPath<Root, Self.Output>, | |
on object: Root) | |
-> AnyCancellable | |
{ | |
var cancellable: AnyCancellable? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension Collection { | |
/// Returns an array of subsequences of maximum size `chunkSize`. | |
/// | |
/// For example: | |
/// | |
/// // ["ABCD", "EFGH", "IJ"] | |
/// "ABCDEFGHIJ".split(chunkSize: 4) | |
/// | |
/// - parameter chunkSize: the maximum size of the returned subsequences. | |
/// - precondition: chunkSize > 0 |