Post

Replies

Boosts

Views

Activity

Reply to @StateObject for view owning "viewModel" to use with @Observable observation framework object
You can see the effect in following code. There are two viewModels, one using ObservableObject and second using @Observable observation framework. The tabView content views owns the viewModel, however when the parent TabView is redrawed, the ownership of @Observation is teared down and new object is recreated on every redraw of TabView @StateObject with ObservableObject maintains its view ownership and the object is not deallocated or recreated as would be expected. @State with @Observable do not follow the ownership, and new object is created every time the parent is redrawed. Following is the code: import Observation import SwiftUI /// ObservableObject, via @StateObject. class ViewModelObservable: ObservableObject { @Published public var title: String deinit { print("deinit ViewModelObservable") } init(title: String) { self.title = title print("init ViewModelObservable") } } /// New Observation framework object. @Observable class ViewModelObseration { public var title: String deinit { print("deinit ViewModelObseration") } init(title: String) { self.title = title print("init ViewModelObseration") } } struct SecondTabContentView: View { /// View Owns object (Observation Framework) @State var viewModelObservation: ViewModelObseration /// View Owns object (Observable Object) @StateObject var viewModelObservable: ViewModelObservable init() { _viewModelObservable = StateObject(wrappedValue: ViewModelObservable(title: "SecondTabContentView Observable")) _viewModelObservation = State(wrappedValue: ViewModelObseration(title: "SecondTabContentView Observation")) } var body: some View { VStack { Text(viewModelObservable.title) Text(viewModelObservation.title) } } } struct FirstTabContentView: View { /// View Owns object (Observation Framework) @State var viewModelObservation: ViewModelObseration /// View Owns object (Observable Object) @StateObject var viewModelObservable: ViewModelObservable init() { _viewModelObservable = StateObject(wrappedValue: ViewModelObservable(title: "FirstTabContentView Observable")) _viewModelObservation = State(wrappedValue: ViewModelObseration(title: "FirstTabContentView Observation")) } var body: some View { VStack { Text(viewModelObservable.title) Text(viewModelObservation.title) } } } struct ContentView: View { @State var tabSelection: Int = 1 @State var redrawTrigger: Bool = false var body: some View { TabView(selection: $tabSelection) { FirstTabContentView() .tag(0) .tabItem { Label("First \(redrawTrigger ? "true" : "false") ", systemImage: "pen") } SecondTabContentView() .tag(1) .tabItem { Label("Second \(redrawTrigger ? "true" : "false")", systemImage: "pen") } } .task { try? await Task.sleep(for: .seconds(3)) self.redrawTrigger = true try? await Task.sleep(for: .seconds(3)) self.redrawTrigger = false } } } #Preview { ContentView() }
Sep ’23
Reply to What is a Location Push Service Extension?
My app is authorized for Location Push, get extension entitlement, and upon calling startMonitoringLocationPushes I am getting a valid token. I am also sending the location Push via token APNS authorization, however the Location Push Extension never wakes up. Any ideas on why the extension is not passing by didReceiveLocationPushPayload(_ payload: [String : Any], completion: @escaping () -> Void) ???
Jan ’22
Reply to Audio Unit v3 - Multiple instances of same AU extension
Hello, there are currently many issues for MIDI FX Plugins and Audio Unit v3: MIDI FX Plugins do not receive the host sample rate, there is no way to inherrit or react to sample rate changes made in Logic Pro X. (This affects severly plugins that calculate midi delta using the event.sampleTime) MIDI FX Plugins that are instantiated as multiple instances on 2 or more tracks, the midi on other instances than selected track are passed and can be seen in plugin instance, however they are not played back later on instrument. This is a serious bug as If anybody instantiate Midi FX Plugin v3 on 2 or more instances, it breaks the playback of these tracks.
Nov ’20
Reply to @FocusedBinding
Thanks for reply and the explanation, this is really great, I hope the issues will be fixed to be able to port apps to use fully SwiftUI. Something like this implementation is really needed as the responder chain do not fit the SwiftUI. Thanks.
Jun ’20