Skip to the content.

ViewScope

MIT License GitHub Release Build Status Swift Version Documentation

Overview

ViewScope is a library that lets you create structured, SwiftUI-friendly async contexts for imperative asynchronous work, such as making a network request when a user taps on a button. It allows you to scope this work to a specific node in the SwiftUI view hiearchy and prevents you from needing to rely on unstructured concurrency to manage task cancellation.

For example:

import ViewScope

struct MyScreen: View {

    @ViewScope myScope
    
    var body: some View {
        VStack {
            Text("This screen can produce async side effects that need to be cancelled if it ever disappears, for example if the user interacts with a deeplink or dismisses a sheet presentation with a swipe")
            Button("Make Network Request") {
                myScope.task {
                    await makeNetworkRequest()
                }
            }
        }
        .whileVisible($myScope)
    }

    func makeNetworkRequest() async {
        // Await things here
    }

}

Installation

ViewScope currently distributed exclusively through the Swift Package Manager.

To add ViewScope as a dependency to an existing Swift package, add the following line of code to the dependencies parameter of your Package.swift file:

dependencies: [
    .package(url: "https://github.com/vsanthanam/ViewScope.git", from: "1.0.0")
]

To add ViewScope as a dependency to an Xcode Project:

Other distribution mechanisms like CocoaPods or Carthage may be added in the future.

Usage & Documentation

ViewScope’s documentation is built with DocC and included in the repository as a DocC archive. The latest version is hosted on GitHub Pages and is available here).

Additional installation instructions are available on the Swift Package Index

Explore the documentation for more details.

License

ViewScope is available under the MIT license. See the LICENSE file for more information.