Skip to content

Latest commit

 

History

History
91 lines (68 loc) · 3.02 KB

usage-slice.md

File metadata and controls

91 lines (68 loc) · 3.02 KB

Slice and OptionalSlice Usage

Slice and OptionalSlice are components of the AppState library that allow you to access specific parts of your application’s state. They are useful when you need to manipulate or observe a part of a more complex state structure.

Overview

  • Slice: Allows you to access and modify a specific part of an existing State object.
  • OptionalSlice: Works similarly to Slice but is designed to handle optional values, such as when part of your state may or may not be nil.

Key Features

  • Selective State Access: Access only the part of the state that you need.
  • Thread Safety: Just like with other state management types in AppState, Slice and OptionalSlice are thread-safe.
  • Reactiveness: SwiftUI views update when the slice of the state changes, ensuring your UI remains reactive.

Example Usage

Using Slice

In this example, we use Slice to access and update a specific part of the state—in this case, the username from a more complex User object stored in the app state.

import AppState
import SwiftUI

struct User {
    var username: String
    var email: String
}

extension Application {
    var user: State<User> {
        state(initial: User(username: "Guest", email: "guest@example.com"))
    }
}

struct SlicingView: View {
    @Slice(\.user, \.username) var username: String

    var body: some View {
        VStack {
            Text("Username: \(username)")
            Button("Update Username") {
                username = "NewUsername"
            }
        }
    }
}

Using OptionalSlice

OptionalSlice is useful when part of your state may be nil. In this example, the User object itself may be nil, so we use OptionalSlice to safely handle this case.

import AppState
import SwiftUI

extension Application {
    var user: State<User?> {
        state(initial: nil)
    }
}

struct OptionalSlicingView: View {
    @OptionalSlice(\.user, \.username) var username: String?

    var body: some View {
        VStack {
            if let username = username {
                Text("Username: \(username)")
            } else {
                Text("No username available")
            }
            Button("Set Username") {
                username = "UpdatedUsername"
            }
        }
    }
}

Best Practices

  • Use Slice for non-optional state: If your state is guaranteed to be non-optional, use Slice to access and update it.
  • Use OptionalSlice for optional state: If your state or part of the state is optional, use OptionalSlice to handle cases where the value may be nil.
  • Thread Safety: Just like with State, Slice and OptionalSlice are thread-safe and designed to work with Swift’s concurrency model.

Conclusion

Slice and OptionalSlice provide powerful ways to access and modify specific parts of your state in a thread-safe manner. By leveraging these components, you can simplify state management in more complex applications, ensuring your UI stays reactive and up-to-date.