Skip to content

Proof of concept for back-porting Observation framework to earlier iOS versions

Notifications You must be signed in to change notification settings

winddpan/ObservationBP

 
 

Repository files navigation

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)

About

Proof of concept for back-porting Observation framework to earlier iOS versions

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C++ 67.3%
  • Swift 32.7%