Skip to content

Instantly share code, notes, and snippets.

private func setupLaunchSteps() {
// Steps are arranged in proper dependency order - DO NOT REORDER without checking dependencies!
// Foundation Layer (no dependencies)
launchSteps.append(AppConfigStep())
launchSteps.append(LoggingConfigurationStep())
launchSteps.append(NetworkMonitoringStep())
launchSteps.append(SecureStorageStep())
launchSteps.append(UserDefaultsStep())
launchSteps.append(AudioSessionStep())
func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
let startTime = CFAbsoluteTimeGetCurrent()
LaunchOrchestrator.shared.executeLaunchSteps() // 🚀
let totalTime = CFAbsoluteTimeGetCurrent() - startTime
print("Total launch time: \(String(format: "%.2f", totalTime))s")
// swift-tools-version: 6.1
import PackageDescription
let package = Package(
name: "SwiftScriptDemos",
platforms: [
.macOS(.v13)
],
dependencies: [
.package(url: "https://github.com/swiftlang/swift-subprocess.git", exact: "0.1.0"),
#!/usr/bin/swift
import Subprocess
let result = try await run(
.name("ls"),
arguments: ["-1"],
output: .string(limit: 1 << 20)
)
print(result.standardOutput ?? "")
while (true) {
char* command = read_input();
pid_t pid = fork();
if (pid == 0) {
exec(command); // execute child process
} else {
wait(pid); // parent waits for child to finish
}
}
@MainActor
func setupUI() async {
await someAsyncWork()
}
@concurrent
func someAsyncWork() async {
// with @concurrent attribute, this runs on the background thread
try? await Task.sleep(for: .milliseconds(100))
}
@MainActor
func setupUI() async {
await someAsyncWork()
}
func someAsyncWork() async {
// Add breakpoint to test this at runtime - it's on the main thread!
try? await Task.sleep(for: .milliseconds(100))
}
actor AuthServiceImpl: AuthService {
var tokenTask: Task<String, Error>?
func getBearerToken() async throws -> String {
if tokenTask == nil {
tokenTask = Task { try await fetchValidAuthToken() }
}
defer { tokenTask = nil }
actor AuthServiceImpl: AuthService {
func getBearerToken() async throws -> String {
if cachedToken.isValid {
return cachedToken
}
return try await authAPI.refreshCredentials() <-- suspension point
}
}
var purgeCacheTask: Task<Void, Never>?
func setupCacheTTL() {
purgeCacheTask?.cancel()
purgeCacheTask = Task(priority: .low) {
try? await Task.sleep(for: .seconds(300))
guard !Task.isCancelled else { return }
cache.clear()
}
}