Skip to content

Instantly share code, notes, and snippets.

// The MIT License (MIT)
//
// Copyright (c) 2020–2022 Alexander Grebenyuk (github.com/kean).
import Pulse
import Foundation
import ObjectiveC.runtime
#if DEBUG
extension Experimental {
actor SomeManager {
private var tasks: [String: DataTask] = [:]
private func data(for request: URLRequest) async throws -> Data {
let requestKey = request.urlRequest?.url?.absoluteString ?? ""
let task = tasks[requestKey] ?? DataTask(task: Task {
try await self._data(for: request, key: requestKey)
})
let subscriptionID = UUID()
task.subscriptions.insert(subscriptionID)
@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 {
CREATE TABLE ZSTATSRECORDVALUE ( Z_PK INTEGER PRIMARY KEY, Z_ENT INTEGER, Z_OPT INTEGER, ZSTATSRECORD INTEGER, ZBESTVIEWSPERDAYCOUNT INTEGER, ZPOSTSCOUNT INTEGER, ZVIEWSCOUNT INTEGER, ZVISITORSCOUNT INTEGER, ZINSIGHTYEAR INTEGER, ZMOSTPOPULARDAYOFWEEK INTEGER, ZMOSTPOPULARDAYOFWEEKPERCENTAGE INTEGER, ZMOSTPOPULARHOUR INTEGER, ZMOSTPOPULARHOURPERCENTAGE INTEGER, ZTOTALCOMMENTSCOUNT INTEGER, ZTOTALIMAGESCOUNT INTEGER, ZTOTALLIKESCOUNT INTEGER, ZTOTALPOSTSCOUNT INTEGER, ZTOTALWORDSCOUNT INTEGER, ZCLICKSCOUNT INTEGER, ZPARENT INTEGER, ZVIEWSCOUNT1 INTEGER, ZDOWNLOADCOUNT INTEGER, ZCOUNT INTEGER, ZTYPE INTEGER, ZTYPE1 INTEGER, ZCOMMENTSCOUNT INTEGER, ZLIKESCOUNT INTEGER, ZPOSTID INTEGER, ZVIEWSCOUNT2 INTEGER, ZOTHERCOUNT INTEGER, ZTOTALCOUNT INTEGER, ZFOLLOWERSCOUNT INTEGER, ZVIEWSCOUNT3 INTEGER, ZPARENT1 INTEGER, ZVIEWSCOUNT4 INTEGER, ZCURRENTSTREAKLENGTH INTEGER, ZLONGESTSTREAKLENGTH INTEGER, ZPOSTCOUNT INTEGER, ZSTREAKINSIGHT INTEGER, ZTYPE2 INTEGER, ZVIEWSCOUNT5 INTEGER, ZPARENT2 INTEGER, ZCOMMENTSCOUNT1 INTEG
@kean
kean / Migration.swift
Created June 20, 2016 20:05
Core Data Progressive Migration
// The MIT License (MIT)
//
// Copyright (c) 2016 Alexander Grebenyuk (github.com/kean).
import Foundation
import CoreData
enum MigrationError: ErrorProtocol {
case IncompatibleModels
}
@kean
kean / CtCI-6h-problem-4.9.markdown
Last active February 24, 2023 14:53
CtCI 6h Edition, Problem 4.9: BST Sequences.

Problem 4.9. BST Sequences: A binary search tree was created by traversing through an array from left to right and inserting each element. Given a binary search tree with distinct elements, print all possible arrays that could have led to this tree.

Solution.

Let's start with an example.

    4
   / \
  2   5 
@kean
kean / ScrollViewPrefetcher.swift
Created February 18, 2023 15:28
ScrollViewPrefetcher
// The MIT License (MIT)
//
// Copyright (c) 2021 Alexander Grebenyuk (github.com/kean).
import SwiftUI
public protocol ScrollViewPrefetcherDelegate: AnyObject {
/// Returns all valid indices for the collection.
func getAllIndicesForPrefetcher(_ prefetcher: ScrollViewPrefetcher) -> Range<Int>
func prefetcher(_ prefetcher: ScrollViewPrefetcher, prefetchItemsAt indices: [Int])
@kean
kean / color-convert.swift
Last active December 25, 2022 14:58
Converts UIColor init to a color literal
import Foundation
/// Input:
///
/// public static let purple = UIColor(red: 74/255, green: 21/255, blue: 153/255, alpha: 1.0)
///
/// Output:
///
/// #4A1599 (74, 21, 153)
/// public static let purple = #colorLiteral(red: 0.2901960784, green: 0.0823529412, blue: 0.6, alpha: 1)
@kean
kean / Formatting.swift
Created October 1, 2022 00:20
Formatting
// The MIT License (MIT)
//
// Copyright (c) 2020 Alexander Grebenyuk (github.com/kean).
import Foundation
#if !os(macOS)
import UIKit
#else
import AppKit
#endif
@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