Skip to content

Instantly share code, notes, and snippets.

View thomsmed's full-sized avatar
:octocat:
✌️😎👌

Thomas Asheim Smedmann thomsmed

:octocat:
✌️😎👌
View GitHub Profile
@thomsmed
thomsmed / AppModuleEnvironmentConfiguration.swift
Created October 6, 2024 19:51
Simple example on how to do initial configuration/setup for different modules in an app on app startup.
//
// AppModuleEnvironmentConfiguration.swift
//
import SwiftUI
// MARK: AppDelegate
final class AppDelegate: NSObject, UIApplicationDelegate {
func application(
@thomsmed
thomsmed / RestrictiveResourceLoader.swift
Created August 20, 2024 17:13
Simple example on how to restrict access to one or more "resources" (local storage, remote endpoint, etc). Very similar to how one would use a semaphore for protected access.
//
// RestrictiveResourceLoader.swift
//
import Foundation
/// Simple example on how to restrict access to one or more "resources" (local storage, remote endpoint, etc).
/// Very similar to how one would use a semaphore for protected access.
final actor RestrictiveResourceLoader {
private var isFetchingResource: Bool = false
@thomsmed
thomsmed / AsyncSemaphore.swift
Created August 18, 2024 14:56
An AsyncSemaphore suited for use in asynchronous contexts. Heavily inspired by https://github.com/groue/Semaphore.
//
// AsyncSemaphore.swift
//
import Foundation
/// An AsyncSemaphore suited for use in asynchronous contexts. Heavily inspired by https://github.com/groue/Semaphore.
public final class AsyncSemaphore: @unchecked Sendable {
final class QueuedContinuation: @unchecked Sendable {
enum State {
@thomsmed
thomsmed / ContinuationCreatingTaskSpawningCancelingResourceCache.swift
Last active March 10, 2024 21:40
A concurrent and thread safe resource cache that spawns an unstructured Task to fetch a resource when the resource is first requested. All Tasks requesting the resource will be suspended, and a Continuation will be created and stored for each Task. Continuations and Tasks will then be resumed once the unstructured resource fetching Task has fini…
//
// ContinuationCreatingTaskSpawningCancelingResourceCache.swift
//
import Foundation
/// A concurrent and thread safe resource cache that spawns an unstructured Task to fetch a resource when the resource is first requested.
/// All Tasks requesting the resource will be suspended, and a Continuation will be created and stored for each Task.
/// Continuations and Tasks will then be resumed once the unstructured resource fetching Task has finished fetching the resource.
/// If a resource requesting Task is canceled while suspended and waiting for the unstructured resource fetching Task,
@thomsmed
thomsmed / ResourceCacheVariants.swift
Last active February 24, 2024 01:23
Various variants of a Resource Cache using primitives from Swift Concurrency.
import Foundation
import Atomics
/// A thread/concurrency context safe value using [Swift Atomics](https://github.com/apple/swift-atomics).
private final class AtomicCount: Sendable {
private let protectedValue = ManagedAtomic<Int>(0)
var value: Int {
protectedValue.load(ordering: .acquiring)
}
@thomsmed
thomsmed / TaskSpawningCancelingResourceCache.swift
Last active March 10, 2024 21:46
A concurrent and thread safe resource cache that spawns an unstructured Task to fetch a resource when the resource is first requested. All Tasks requesting the resource will wait on the completion of the spawned unstructured resource fetching Task. If all resource requesting Tasks are canceled, the resource cache will also cancel the spawned uns…
//
// TaskSpawningCancelingResourceCache.swift
//
import Foundation
import Atomics
/// A thread/concurrency context safe value using [Swift Atomics](https://github.com/apple/swift-atomics).
/// Heavily inspired by this WWDC 2023 video: [Beyond the basics of structured concurrency](https://developer.apple.com/videos/play/wwdc2023/10170/).
final class AtomicCount: Sendable {
@thomsmed
thomsmed / TaskSpawningResourceCache.swift
Last active March 10, 2024 21:42
A concurrent and thread safe resource cache that spawns an unstructured Task to fetch a resource when the resource is first requested. All Tasks requesting the resource will wait on the completion of the spawned unstructured resource fetching Task.
//
// TaskSpawningResourceCache.swift
//
import Foundation
/// A concurrent and thread safe resource cache that spawns an unstructured Task to fetch a resource when the resource is first requested.
/// All Tasks requesting the resource will wait on the completion of the spawned unstructured resource fetching Task.
final actor TaskSpawningResourceCache {
private let urlSession: URLSession
@thomsmed
thomsmed / ContinuationCreatingCancelingResourceCache.swift
Last active March 10, 2024 21:45
A concurrent and thread safe resource cache that will make the first resource requesting Task fetch the resource asynchronously while being suspended. All subsequent Tasks requesting the resource will also be suspended, and a Continuation will be created and stored for each Task. Continuations and Tasks will then be resumed once the initial Task…
//
// ContinuationCreatingCancelingResourceCache.swift
//
import Foundation
/// A thread/concurrency context safe storage for Continuations using [NSLock](https://developer.apple.com/documentation/foundation/nslock).
/// Heavily inspired by this WWDC 2023 video: [Beyond the basics of structured concurrency](https://developer.apple.com/videos/play/wwdc2023/10170/).
///
/// NOTE: Be careful to always resume any suspended Continuations to prevent Continuation / Task leaks.
@thomsmed
thomsmed / ContinuationCreatingResourceCache.swift
Last active March 10, 2024 21:44
A concurrent and thread safe resource cache that will make the first resource requesting Task fetch the resource asynchronously while being suspended. All subsequent Tasks requesting the resource will also be suspended, and a Continuation will be created and stored for each Task. Continuations and Tasks will then be resumed once the initial Task…
//
// ContinuationCreatingResourceCache.swift
//
import Foundation
/// A thread/concurrency context safe storage for Continuations using [NSLock](https://developer.apple.com/documentation/foundation/nslock).
/// Heavily inspired by this WWDC 2023 video: [Beyond the basics of structured concurrency](https://developer.apple.com/videos/play/wwdc2023/10170/).
///
/// NOTE: Be careful to always resume any suspended Continuations to prevent Continuation / Task leaks.
@thomsmed
thomsmed / RunLoopSignaling.swift
Created February 12, 2024 16:48
Snippet showing how to configure a simple custom CFRunLoopSource.
//
// StopRunLoopSource.swift
//
import Foundation
final class StopRunLoopSource: NSObject {
// More on how to define custom run loop input sources: https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/Multithreading/RunLoopManagement/RunLoopManagement.html#//apple_ref/doc/uid/10000057i-CH16-SW3
// And tips on how to terminate threads: https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/Multithreading/CreatingThreads/CreatingThreads.html#//apple_ref/doc/uid/10000057i-CH15-SW10