Skip to content

Instantly share code, notes, and snippets.

@kean
kean / Client.swift
Last active September 16, 2022 03:41
API Client (Archived)
// The MIT License (MIT)
//
// Copyright (c) 2017 Alexander Grebenyuk (github.com/kean).
import Foundation
import Alamofire
import RxSwift
import RxCocoa
// This post is **archived**. For a modern version that uses Async/Await and Actors, see the new article
@kean
kean / CancellationToken.swift
Last active December 17, 2017 14:52
Cancellation Token in Swift
// The MIT License (MIT)
//
// Copyright (c) 2017 Alexander Grebenyuk (github.com/kean).
import Foundation
/// Manages cancellation tokens and signals them when cancellation is requested.
///
/// All `CancellationTokenSource` methods are thread safe.
public final class CancellationTokenSource {
@kean
kean / AutoRetry.swift
Last active September 20, 2023 20:21
Smart Auto Retry using RxSwift
// The MIT License (MIT)
//
// Copyright (c) 2017 Alexander Grebenyuk (github.com/kean).
import Foundation
import RxSwift
import RxCocoa
extension ObservableType {
@kean
kean / spacers.swift
Last active January 10, 2018 18:12
Stacks and Spacers
public typealias Stack = UIStackView
public extension Stack {
@nonobjc public convenience init(_ views: UIView..., with: (UIStackView) -> Void = { _ in }) {
self.init(arrangedSubviews: views)
with(self)
}
@nonobjc public convenience init(_ views: [UIView], axis: UILayoutConstraintAxis = .horizontal, spacing: CGFloat = 0, alignment: UIStackViewAlignment = .fill, distribution: UIStackViewDistribution = .fill) {
self.init(arrangedSubviews: views)
@kean
kean / CancellationToken.swift
Last active November 15, 2018 19:23
CancellationToken
// MARK: - CancellationTokenSource
/// Manages cancellation tokens and signals them when cancellation is requested.
///
/// All `CancellationTokenSource` methods are thread safe.
final class CancellationTokenSource {
/// Returns `true` if cancellation has been requested.
var isCancelling: Bool {
_lock.lock(); defer { _lock.unlock() }
return _observers == nil
@kean
kean / AsyncAwaitTest.swift
Last active November 24, 2018 22:47
Fake (blocking) Async/Await for https://github.com/kean/FutureX
class AsyncAwaitTests: XCTestCase {
func testAsyncAwait() {
XCTAssertEqual(fakeAsyncAwait().wait().value, 3)
}
}
func fakeAsyncAwait() -> Future<Int, Error> {
return Future.async {
// Add delay to demonstrate that `await` for `Future<_, Never>` don't
// Configuration settings file format documentation can be found at:
// https://help.apple.com/xcode/#/dev745c5c974
SUPPORTED_PLATFORMS = iphoneos macosx appletvos watchos iphonesimulator appletvsimulator watchsimulator
SWIFT_VERSION = 4.2
IPHONEOS_DEPLOYMENT_TARGET = 9.0
MACOSX_DEPLOYMENT_TARGET = 10.11
@kean
kean / FutureCompatible.swift
Created November 20, 2019 12:22
FutureCompatible.swift
// The MIT License (MIT)
//
// Copyright (c) 2016-2019 Alexander Grebenyuk (github.com/kean).
import Foundation
/// Future extenions.
public struct FutureExtension<Base> {
/// Base object to extend.
public let base: Base
// The MIT License (MIT)
//
// Copyright (c) 2020 Alexander Grebenyuk (github.com/kean).
import CoreData
final class FetchedEntities<Element: NSManagedObject>: NSObject, NSFetchedResultsControllerDelegate, RandomAccessCollection, ObservableObject {
private let controller: NSFetchedResultsController<Element>
init(context: NSManagedObjectContext, request: NSFetchRequest<Element>) {
@kean
kean / _DynamicProperty.swift
Last active February 23, 2020 22:47
_DynamicProperty
// A speculative _ViewRendererHost implementation which uses Mirror (Swift reflection) to find all
// of the dynamic properties (`DynamicProperty`) associated with a given view (`View`), observe
// the changes to these properties and automatically refresh the view whenever any of these property change.
protocol _DynamicProperty {
var objectWillChange: AnyPublisher<Void, Never> { get }
}
extension ObservedObject: _DynamicProperty {
var objectWillChange: AnyPublisher<Void, Never> {