Skip to main content
Swift package · Open source

FlowKit

Kotlin Flow semantics in Swift, with an API that reads like Swift.
let articles: ThrowingFlow<[Article]> = ThrowingFlow { collector in
    // Emit cached articles first so the UI has something to show
    let cached = try await articleDatabase.fetchAll()
    try await collector.emit(cached)

    // Then fetch fresh articles from the network
    let fresh = try await articleAPI.fetchLatest()
    try await articleDatabase.upsert(fresh)
    try await collector.emit(fresh)
}

try await articles
    .map { articles in articles.filter { !$0.isArchived } }
    .collect { visibleArticles in
        articleListView.display(visibleArticles)
    }
An offline-first feed: cached articles first, fresh ones when they land.
Why it exists

Flow, but Swift

Kotlin Flow is a good way to think about streams: cold until someone collects, hot when you need to share, and operators that read top to bottom. FlowKit takes that model and gives it a Swift API. It doesn't copy Kotlin's names where Swift has its own, and it's only Flow, not the whole of Kotlin's coroutines library. If you're an Android developer coming to iOS, Flow is probably how you already think about streams. FlowKit means you don't have to give that up when you write Swift: cold and hot flows, operators, and tests that run on virtual time.
Two kinds of stream

Cold by default, hot when you share

Cold

Flow · ThrowingFlow

Each collector gets its own run of the body. Nothing happens until something collects.

Hot

StateFlow · SharedFlow

StateFlow holds a current value and skips duplicates. SharedFlow broadcasts to every subscriber, with a replay buffer you choose.
Turn a cold flow hot with asStateFlow or asSharedFlow, and pick when it starts: eagerly, lazily, or only while someone's subscribed.
Operators

The ones you reach for

  • Transform

    • map
    • compactMap
    • filter
    • transform
    • prefix
    • dropFirst
    • scan
  • Lifecycle

    • onStart
    • onEach
    • onCompletion
  • Flattening

    • flatMap
    • flatMap(maxConcurrent:)
    • flatMapLatest
  • Combining

    • zip
    • combineLatest
    • merge
  • Errors

    • catch
    • retry
    • retryWhen
  • Rate limiting

    • debounce
    • throttle
    • removeDuplicates
    • sample
  • Buffering

    • buffer
    • keepingLatest
  • Terminal

    • collectLatest
    • first
    • exactlyOne
    • toArray
    • reduce
Testing

Tests that can't time out on a slow machine

probing reads a flow with no timeout. Each read waits for the next value, the completion or the failure, so a slow CI machine can't fail a correct test. When a test really does need time, TestClock gives you virtual time and you move it by hand. No sleeps.
import Testing
import Flow
import FlowTesting

@Test("article feed emits cached then fresh")
func articleFeedEmitsCachedThenFresh() async throws {
    let feed = ThrowingFlow<[Article]> { collector in
        try await collector.emit([.cached])
        try await collector.emit([.cached, .fresh])
    }

    try await feed.probing { reader in
        try await reader.expectValue([.cached])
        try await reader.expectValue([.cached, .fresh])
        try await reader.expectCompletion()
    }
}
Swift 6

Races show up as build errors

Every target builds in Swift 6 language mode with strict concurrency on. If a value crossing a thread or actor boundary isn't safe to share, the build fails. That kind of data race turns into a compile error where you wrote the code, not a crash report from wherever it happened to run.
FlowUI

Plugs into SwiftUI and UIKit

For SwiftUI there's the @CollectedState property wrapper, ObservedStateFlow and View.collecting, plus Flow(observing:) for @Observable types. On UIKit and AppKit, flowScope ties collection to a view controller's lifetime.

Runs on

  • iOS 16+
  • macOS 13+
  • tvOS 16+
  • watchOS 9+
  • visionOS 1+
  • Android (build)
Needs Swift 6.3 or later (Xcode 26.4 or later). Flow and FlowTesting also build for Android with the Swift SDK for Android, and every pull request has to pass that build. The tests run on an Android emulator every night, but those runs don't block anything, so test on your own devices if you ship it there. FlowUI is Apple only.
Install

Add it with Swift Package Manager

dependencies: [
    .package(url: "https://github.com/DeveloperBeau/FlowKit", from: "2.0.0")
]
Add Flow to your app target and FlowTesting to your test target. It's MIT licensed.
  • Swift 6
  • Strict concurrency
  • Swift Concurrency
  • SwiftUI
  • UIKit and AppKit
  • Swift Testing
  • DocC
  • Swift SDK for Android
  • MIT
Back to home