Skip to content

Instantly share code, notes, and snippets.

View groue's full-sized avatar

Gwendal Roué groue

View GitHub Profile
@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 / 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)
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
//: # 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
@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 {
@import UIKit;
@interface UIButton(Magic)
- (void)onTouchUpInside:(void(^)(UIButton *button))block NS_SWIFT_NAME(onTouchUpInside(_:));
@end
@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

Keybase proof

I hereby claim:

  • I am groue on github.
  • I am groue (https://keybase.io/groue) on keybase.
  • I have a public key whose fingerprint is EDC5 4AA4 0D3A 475A DB0E 78B4 2A35 40DD A1C7 3F08

To claim this, I am signing this object:

@groue
groue / ReadWriteBox.swift
Created November 18, 2016 15:43
A ReadWriteBox grants multiple readers and single-writer guarantees on a value.
/// A ReadWriteBox grants multiple readers and single-writer guarantees on a value.
final class ReadWriteBox<T> {
var value: T {
get { return read { $0 } }
set { write { $0 = newValue } }
}
init(_ value: T) {
self._value = value
self.queue = DispatchQueue(label: "ReadWriteBox", attributes: [.concurrent])
class PersonsViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Configure the navigation bar
navigationItem.rightBarButtonItems = [
UIBarButtonItem(
barButtonSystemItem: .Add,
target: self,
action: #selector(PersonsViewController.addPerson(_:))),