Skip to content

Latest commit

 

History

History
75 lines (61 loc) · 2.03 KB

README.md

File metadata and controls

75 lines (61 loc) · 2.03 KB

Demo

Example

import ObservationBP
import SwiftUI

@Observable final class Person {
    var name: String
    var age: Int

    init(name: String, age: Int) {
        self.name = name
        self.age = age
    }
}

struct ContentView: View {
    @Observing @State var person: Person = Person(name: "name", age: 1)

    var body: some View {
        VStack {
            PersonNameView(person: person)
            PersonAgeView(person: person)
        }
    }
}

private struct PersonNameView: View {
    @Observing var person: Person

    var body: some View {
        Text(person.name)
            .foregroundColor(Color(
                red: .random(in: 0 ... 1),
                green: .random(in: 0 ... 1),
                blue: .random(in: 0 ... 1)
            ))
    }
}

private struct PersonAgeView: View {
    @Observing var person: Person

    var body: some View {
        Text("age \(person.age)")
            .foregroundColor(Color(
                red: .random(in: 0 ... 1),
                green: .random(in: 0 ... 1),
                blue: .random(in: 0 ... 1)
            ))
    }
}

Installation

Swift Package Manager

https://github.com/winddpan/ObservationBP.git

Based on

onevcat/ObservationBP

Improvement

  • No more ObservationView.
    • Delay closure without ObservationView either.
    • Use @Observing once in each View
  • Instance kept, similar to @State and @StateObject.
  • Memory leak fixed.

Usage

Observation ObservationBP
var person: Person @Observing var person: Person
@State var person = Person(name: "name", age: 1) @Observing @State var person = Person(name: "name", age: 1)