Skip to content

Instantly share code, notes, and snippets.

View groue's full-sized avatar

Gwendal Roué groue

View GitHub Profile
@groue
groue / JSONSynchronization.swift
Created September 13, 2017 14:33
GRDB JSONSynchronization
// 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
@import UIKit;
@interface UIButton(Magic)
- (void)onTouchUpInside:(void(^)(UIButton *button))block NS_SWIFT_NAME(onTouchUpInside(_:));
@end
@groue
groue / ContextFetchableRecord.swift
Last active May 6, 2018 15:48
ContextFetchableRecord: a new realm of records for GRDB.swift
// 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 {
//: # 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
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
@groue
groue / CancelBag.swift
Last active April 5, 2024 19:12
CancelBag
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)
@groue
groue / AsynchronousOperation.swift
Last active October 30, 2020 13:11
AsynchronousOperation
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 {
@groue
groue / GRDB+DeleteAllOther.swift
Last active June 15, 2020 15:14
GRDB request.deleteAllOther(db)
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)
@groue
groue / Combine+Weak.swift
Created June 15, 2020 08:09
Combine assignWeakly
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?
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