Skip to content

Instantly share code, notes, and snippets.

@alemar11
alemar11 / AsyncOperation.swift
Created December 6, 2018 10:10 — forked from ole/AsyncOperation.swift
An (NS)Operation subclass for async operations
import Foundation
/// An abstract class that makes building simple asynchronous operations easy.
/// Subclasses must override `main()` to perform any work and call `finish()`
/// when they are done. All `NSOperation` work will be handled automatically.
///
/// Source/Inspiration: https://stackoverflow.com/a/48104095/116862 and https://gist.github.com/calebd/93fa347397cec5f88233
open class AsyncOperation: Operation {
public init(name: String? = nil) {
super.init()
@alemar11
alemar11 / Debouncer.swift
Created November 29, 2018 09:36
Debouncer
/// Enforces a function to not be called again until a certain amount of time has passed without it being called.
/// As in "execute this function only if 100 milliseconds have passed without it being called."
public final class Debouncer {
// MARK: - Properties
public let limit: DispatchTimeInterval
public let queue: DispatchQueue
private var workItem: DispatchWorkItem?
@alemar11
alemar11 / GCDMulticastDelegate.swift
Created November 28, 2018 19:29
MulticastDelegate
open class GCDMulticastDelegate<T> {
private var nodes = Set<Node<AnyObject>>()
public init() { }
deinit {
removeAllDelegates()
}
@alemar11
alemar11 / SpinlockTestTests.swift
Created November 20, 2018 11:55 — forked from steipete/SpinlockTestTests.swift
Updated for Xcode 8, Swift 3; added os_unfair_lock
//
// SpinlockTestTests.swift
// SpinlockTestTests
//
// Created by Peter Steinberger on 04/10/2016.
// Copyright © 2016 PSPDFKit GmbH. All rights reserved.
//
import XCTest
import Foundation
final class SafeSyncQueue {
struct QueueIdentity {
let label: String
}
let queue: DispatchQueue
/// Manager of asynchronous download `Operation` objects
class DownloadManager: NSObject {
/// Dictionary of operations, keyed by the `taskIdentifier` of the `URLSessionTask`
fileprivate var operations = [Int: DownloadOperation]()
/// Serial NSOperationQueue for downloads
@alemar11
alemar11 / rangeFromNSRange.swift
Created July 14, 2018 13:43
Range from NSRange
extension String {
@available(*, deprecated, message: "Swift 4 supports conversion between NSRange and Range ( Range.init?(_:in) )")
private func range(from nsRange: NSRange) -> Range<Index>? {
guard let range = Range(nsRange) else { return nil }
let utf16Start = UTF16Index(encodedOffset: range.lowerBound)
let utf16End = UTF16Index(encodedOffset: range.upperBound)
guard
let start = Index(utf16Start, within: self),
import Foundation
final class ConcealingTitleView: UIView {
private let label = UILabel()
private var contentOffset: CGFloat = 0 {
didSet {
label.frame.origin.y = titleVerticalPositionAdjusted(by: contentOffset)
}
}
var text: String = "" {
@alemar11
alemar11 / Async.swift
Created July 3, 2018 12:53 — forked from brennanMKE/Async.swift
Blocking with Semaphores and DispatchGroups in Swift
import PlaygroundSupport
import Foundation
class Worker {
private let queue = DispatchQueue.global(qos: .background)
private let serialQueue = DispatchQueue(label: "com.acme.serial")
public private(set) var count = 0
func incrementCount() {
func time() -> DispatchTimeInterval {
//https://stackoverflow.com/questions/12488481/getting-ios-system-uptime-that-doesnt-pause-when-asleep
var uptime = timespec()
precondition(clock_gettime(CLOCK_MONOTONIC_RAW, &uptime) == 0, "Could not execute clock_gettime, errno: \(errno)")
let nanoseconds = (uptime.tv_sec * 1_000_000_000) + uptime.tv_nsec
return .nanoseconds(nanoseconds)
}