Thanks for being a part of WWDC25!

How did we do? We’d love to know your thoughts on this year’s conference. Take the survey here

Observation

RSS for tag

Make responsive apps that update the presentation when underlying data changes.

Posts under Observation tag

41 Posts
Sort by:

Post

Replies

Boosts

Views

Activity

@Observable with generic typed throw breaks SwiftCompile
@Observable seems not to work well with generic typed throw. The following code using @Observable with non-generic typed throw builds good: @Observable class ThrowsLoadingViewModel<R, E: Error> { private(set) var isLoading = true private(set) var error: E? = nil private(set) var data: R? = nil private var task: () throws(Error) -> R init(task: @escaping () throws(E) -> R) { self.task = task } func load() { do throws(Error) { self.data = try task() } catch { // self.error = error } self.isLoading = false } } But if I change Line 7 and 14 to generic, it'll breaks the build with a "Command SwiftCompile failed with a nonzero exit code" message : @Observable class ThrowsLoadingViewModel<R, E: Error> { private(set) var isLoading = true private(set) var error: E? = nil private(set) var data: R? = nil private var task: () throws(E) -> R init(task: @escaping () throws(E) -> R) { self.task = task } func load() { do throws(E) { self.data = try task() } catch { // self.error = error } self.isLoading = false } } A the same time, if I remove @Observable, the generic typed throw works again: class ThrowsLoadingViewModel<R, E: Error> { private(set) var isLoading = true private(set) var error: E? = nil private(set) var data: R? = nil private var task: () throws(E) -> R init(task: @escaping () throws(E) -> R) { self.task = task } func load() { do throws(E) { self.data = try task() } catch { // self.error = error } self.isLoading = false } } Currently the possible solution seems to fall back to use ObservableObject...
0
0
11
2d
Disabling UIKit observation tracking?
The "What's new in UIKit" session introduces new observation tracking features and mentions that they are "on by default" in 26. Is it possible to disable this feature? We have our own system built on ObservableObject that keeps our UIKit models/views in sync and triggers updates. We want to make sure there isn't contention between the new feature and our own.
1
1
44
2d
Using Observation class for multiple SwiftData Models
Greetings i have an app that uses three different SwiftData models and i want to know what is the best way to use the them accross the app. I though a centralized behaviour and i want to know if it a correct approach.First let's suppose that the first view of the app will load the three models using the @Enviroment that work with @Observation. Then to other views that add data to the swiftModels again with the @Environment. Another View that will use the swiftData models with graph and datas for average and min and max.Is this a corrent way? or i should use @Query in every view that i want and ModelContext when i add the data. @Observable class CentralizedDataModels { var firstDataModel: [FirstDataModel] = [] var secondDataModel: [SecondDataModel] = [] var thirdDataModel: [ThirdDataModel] = [] let context: ModelContext init(context:ModelContext) { self.context = context } }
0
0
60
2w
Mixing ReferenceFileDocument and @Observable
I have an app in which the data model is @Observable, and views see it through @Environment(dataModel.self) private var dataModel. Since there are a large number of views, only some of which may need to be redrawn at a given time, I believe that @Observable is more efficient at run time than @Published and @ObservedObject I’ve been trying to make the app document based. Although I started using SwiftData, it has trouble with Codable, and a long thread in the Developer forum suggests that SwiftData does not support the Undo manager - and in any event, simple JSON serialization is all that this app requires. Unfortunately, ReferenceFileDocument inherits from ObservableObject, which seems to not play nice with @Observable. I’d like to keep using @Observable, but haven’t been able to figure out how. When I deserialize a JSON ReferenceFileDocument, I can’t seem to connect it to an @Observable class instance and to let the various views and view models know where to find and update it. I’d appreciate advice on how to implement document persistence in this app. Also, the default behaviour of DoumentGroup provides a nice menu to, another things, rename a new file to something other than Untitled xx, but it doesn’t appear to work (there is an extensive thread on the Developer website discussing this issue). Is there a solution to this problem? Thanks for any help you can offer.
3
0
82
3w
What is the proper way to format a generic subview view when hoping to have it work with both an @State and an @Bindable?
Let's say you have a protocol that can work with both classes and structs but you want to have a uniform UI to make edits. What is the recommended way to have one view that will take both? App import SwiftUI @main struct ObservationTesterApp: App { var body: some Scene { WindowGroup { ContentView(existence: Existence()) } } } Types import Foundation protocol Dateable { var timestamp:Date { get set } } struct Arrival:Dateable { var timestamp:Date } @Observable class Existence:Dateable { var timestamp:Date init(timestamp: Date) { self.timestamp = timestamp } } extension Existence { convenience init() { self.init(timestamp: Date()) } } ContentView, etc // // ContentView.swift // ObservationTester // // import SwiftUI struct EditDateableView<TimedThing:Dateable>:View { @Binding var timed:TimedThing //note that this currently JUST a date picker //but it's possible the protocol would have more var body:some View { DatePicker("Time To Change", selection: $timed.timestamp) } } #Preview { @Previewable @State var tt = Arrival(timestamp: Date()) EditDateableView<Arrival>(timed: $tt) } struct ContentView: View { @State var arrival = Arrival(timestamp: Date()) @Bindable var existence:Existence var body: some View { //this work around also not allowed. "self is immutable" // let existBinding = Binding<Existence>(get: { existence }, set: { existence = $0 }) VStack { EditDateableView(timed: $arrival) //a Binding cant take a Bindable //EditDateableView<Existence>(timed: $existence) } .padding() } } #Preview { ContentView(existence: Existence()) }
0
0
68
May ’25
Custom @Observable RandomAcccessCollection List/ForEach issues
I'm trying to understand the behavior I'm seeing here. In the following example, I have a custom @Observable class that adopts RandomAccessCollection and am attempting to populate a List with it. If I use an inner collection property of the instance (even computed as this shows), the top view identifies additions to the list. However, if I just use the list as a collection in its own right, it detects when a change is made, but not that the change increased the length of the list. If you add text that has capital letters you'll see them get sorted correctly, but the lower list retains its prior count. The choice of a List initializer with the model versus an inner ForEach doesn't change the outcome, btw. If I cast that type as an Array(), effectively copying its contents, it works fine which leads me to believe there is some additional Array protocol conformance that I'm missing, but that would be unfortunate since I'm not sure how I would have known that. Any ideas what's going on here? The new type can be used with for-in scenarios fine and compiles great with List/ForEach, but has this issue. I'd like the type to not require extra nonsense to be used like an array here. import SwiftUI fileprivate struct _VExpObservable6: View { @Binding var model: ExpModel @State private var text: String = "" var body: some View { NavigationStack { VStack(spacing: 20) { Spacer() .frame(height: 40) HStack { TextField("Item", text: $text) .textFieldStyle(.roundedBorder) .textContentType(.none) .textCase(.none) Button("Add Item") { guard !text.isEmpty else { return } model.addItem(text) text = "" print("updated model #2 using \(Array(model.indices)):") for s in model { print("- \(s)") } } } InnerView(model: model) OuterView(model: model) } .listStyle(.plain) .padding() } } } // - displays the model data using an inner property expressed as // a collection. fileprivate struct InnerView: View { let model: ExpModel var body: some View { VStack { Text("Model Inner Collection:") .font(.title3) List { ForEach(model.sorted, id: \.self) { item in Text("- \(item)") } } .border(.darkGray) } } } // - displays the model using the model _as the collection_ fileprivate struct OuterView: View { let model: ExpModel var body: some View { VStack { Text("Model as Collection:") .font(.title3) // - the List/ForEach collections do not appear to work // by default using the @Observable model (RandomAccessCollection) // itself, unless it is cast as an Array here. List { // ForEach(Array(model), id: \.self) { item in ForEach(model, id: \.self) { item in Text("- \(item)") } } .border(.darkGray) } } } #Preview { @Previewable @State var model = ExpModel() _VExpObservable6(model: $model) } @Observable fileprivate final class ExpModel: RandomAccessCollection { typealias Element = String var startIndex: Int { 0 } var endIndex: Int { sorted.count } init() { _listData = ["apple", "yellow", "about"] } subscript(_ position: Int) -> String { sortedData()[position] } var sorted: [String] { sortedData() } func addItem(_ item: String) { _listData.append(item) _sorted = nil } private var _listData: [String] private var _sorted: [String]? private func sortedData() -> [String] { if let ret = _sorted { return ret } let ret = _listData.sorted() _sorted = ret return ret } }
1
0
56
Apr ’25
Using `@ObservedObject` in a function
No real intruduction for this, so I'll get to the point: All this code is on GitHub: https://github.com/the-trumpeter/Timetaber-for-iWatch But first, sorry; /* I got roasted, last time I posted; for not defining my stuff. This'll be different, but's gonna be rough; 'cuz there's lots and lots to get through: */ //this is 'Timetaber Watch App/Define (No expressions)/Courses_vDef.swift' on the GitHub: struct Course { let name: String let icon: String let room: String let colour: String let listName: String let listIcon: String let joke: String init(name: String, icon: String, room: String? = nil, colour: String, listName: String? = nil, listIcon: String? = nil, joke: String? = nil) { self.name = name self.icon = icon self.room = room ?? "None" self.colour = colour self.listName = listName ?? name self.listIcon = listIcon ?? (icon+".circle.fill") self.joke = joke ?? "" } } //this is 'Timetaber Watch App/TimeManager_fDef.swift' on the GitHub: func getCurrentClass(date: Date) -> Array<Course> { //returns the course in session depending on the input date //it is VERY long but //all you really need to know is what it returns: //basically: return [rightNow, nextUp] } /* I thought that poetry would be okay, But poorly thought things through: For I'll probably find that people online will treat my rhymes like spew. */ So into the question: I have a bunch of views, all (intendedly) watching two variables inside of a class: //Github: 'Timetaber Watch App/TimetaberApp.swift' class GlobalData: ObservableObject { @Published var currentCourse: Course = getCurrentClass(date: .now)[0] // the current timetabled class in session. @Published var nextCourse: Course = getCurrentClass(date: .now)[1] // the next timetabled class in session } ...and a bunch of views using them in different ways as follows: (Sorry, don't have the characters to define functions called in these) import SwiftUI //Github: 'Timetaber Watch App/Views/HomeView.swift' struct HomeView: View { @StateObject var data = GlobalData() var body: some View { //HERE: let icon = data.currentCourse.icon let name = data.currentCourse.name let colour = data.currentCourse.colour let room = roomOrBlank(course: data.currentCourse) let next = data.nextCourse VStack { //CURRENT CLASS Image(systemName: icon) .foregroundColor(Color(colour))//add an SF symbol element .imageScale(.large) .font(.system(size: 25).weight(.semibold)) Text(name) .font(.system(size:23).weight(.bold)) .foregroundColor(Color(colour)) .padding(.bottom, 0.1) //ROOM Text(room+"\n") .multilineTextAlignment(.center) .foregroundStyle(.gray) .font(.system(size: 15)) if next.name != noSchool.name { Spacer() //NEXT CLASS Text(nextPrefix(course: next)) .font(.system(size: 15)) Text(getNextString(course: next)) .font(.system(size: 15)) .multilineTextAlignment(.center) } }.padding() } } // Github: 'Timetaber Watch App/Views/ListView.swift' struct listTemplate: View { @StateObject var data = GlobalData() var listedCourse: Course = failCourse(feedback: "lT.12") var courseTime: String = "" init(course: Course, courseTime: String) { self.courseTime = courseTime self.listedCourse = course } var body: some View { let localroom = if listedCourse.room == "None" { "" } else { listedCourse.room } let image = if listedCourse.listIcon == "custom1" { Image(.paintbrushPointedCircleFill) } else { Image(systemName: listedCourse.listIcon) } HStack{ image .foregroundColor(Color(listedCourse.colour)) .padding(.leading, 5) Text(listedCourse.name) .bold() Spacer() Text(courseTime) Text(localroom).bold().padding(.trailing, 5) } .padding(.bottom, 1) .background(data.currentCourse.name==listedCourse.name ? Color(listedCourse.colour).colorInvert(): nil) //HERE } } struct listedDay: View { let day: Dictionary<Int, Course> var body: some View { let dayKeys = Array(day.keys).sorted(by: <) List { ForEach((0...dayKeys.count-2), id: \.self) { let num = $0 listTemplate(course: day[dayKeys[num]] ?? failCourse(feedback: "lD.53"), courseTime: time24toNormal(time24: dayKeys[num])) } } } } struct ListView: View { var body: some View { if storage.shared.termRunningGB && weekdayFunc(inDate: .now) != 1 && weekdayFunc(inDate: .now) != 7 { ScrollView { listedDay( day: getTimetableDay( isWeekA: getIfWeekIsA_FromDateAndGhost( originDate: .now, ghostWeek: storage.shared.ghostWeekGB ), weekDay: weekdayFunc(inDate: .now) ) ) } } else if !storage.shared.termRunningGB { Text("There's no term running.\nThe day's classes will be displayed here.") .multilineTextAlignment(.center) .foregroundStyle(.gray) .font(.system(size: 13)) } else { Text("No school today.\nThe day's classes will be displayed here.") .multilineTextAlignment(.center) .foregroundStyle(.gray) .font(.system(size: 13)) } } } //There's one more view but I can't fit it for characters. //On GitHub: 'Timetaber Watch App/Views/SettingsView.swift' So... THE FUNCTION: This function is called when changes are made that will affect the correct output of getCurrentClass. It is intended to reload the views and the current/next variables to reflect those changes.\ //GHub: 'Timetaber Watch App/StorageManager.swift' func reload() -> Void { @ObservedObject var globalData: GlobalData //this line is erroring, I don't know how to fix it. Is this even the best/proper way to do this? let courseData = getCurrentClass(date: .now) globalData.currentCourse = courseData[0] globalData.nextCourse = courseData[1] //Variable '_globalData' used by function definition before being initialized //that is the error appearing on those above two redefinitions. print("Setup done\n") } Thanks! -Gill
1
0
244
Mar ’25
How to implement thread-safe property wrapper notifications across different contexts in Swift?
I’m trying to create a property wrapper that that can manage shared state across any context, which can get notified if changes happen from somewhere else. I'm using mutex, and getting and setting values works great. However, I can't find a way to create an observer pattern that the property wrappers can use. The problem is that I can’t trigger a notification from a different thread/context, and have that notification get called on the correct thread of the parent object that the property wrapper is used within. I would like the property wrapper to work from anywhere: a SwiftUI view, an actor, or from a class that is created in the background. The notification preferably would get called synchronously if triggered from the same thread or actor, or otherwise asynchronously. I don’t have to worry about race conditions from the notification because the state only needs to reach eventuall consistency. Here's the simplified pseudo code of what I'm trying to accomplish: // A single source of truth storage container. final class MemoryShared<Value>: Sendable { let state = Mutex<Value>(0) func withLock(_ action: (inout Value) -> Void) { state.withLock(action) notifyObservers() } func get() -> Value func notifyObservers() func addObserver() } // Some shared state used across the app static let globalCount = MemoryShared<Int>(0) // A property wrapper to access the shared state and receive changes @propertyWrapper struct SharedState<Value> { public var wrappedValue: T { get { state.get() } nonmutating set { // Can't set directly } } var publisher: Publisher {} init(state: MemoryShared) { // ... } } // I'd like to use it in multiple places: @Observable class MyObservable { @SharedState(globalCount) var count: Int } actor MyBackgroundActor { @SharedState(globalCount) var count: Int } @MainActor struct MyView: View { @SharedState(globalCount) var count: Int } What I’ve Tried All of the examples below are using the property wrapper within a @MainActor class. However the same issue happens no matter what context I use the wrapper in: The notification callback is never called on the context the property wrapper was created with. I’ve tried using @isolated(any) to capture the context of the wrapper and save it to be called within the state in with unchecked sendable, which doesn’t work: final class MemoryShared<Value: Sendable>: Sendable { // Stores the callback for later. public func subscribe(callback: @escaping @isolated(any) (Value) -> Void) -> Subscription } @propertyWrapper struct SharedState<Value> { init(state: MemoryShared<Value>) { MainActor.assertIsolated() // Works! state.subscribe { MainActor.assertIsolated() // Fails self.publisher.send() } } } I’ve tried capturing the isolation within a task with AsyncStream. This actually compiles with no sendable issues, but still fails: @propertyWrapper struct SharedState<Value> { init(isolation: isolated (any Actor)? = #isolation, state: MemoryShared<Value>) { let (taskStream, continuation) = AsyncStream<Value>.makeStream() // The shared state sends new values to the continuation. subscription = state.subscribe(continuation: continuation) MainActor.assertIsolated() // Works! let task = Task { _ = isolation for await value in taskStream { _ = isolation MainActor.assertIsolated() // Fails } } } } I’ve tried using multiple combine subjects and publishers: final class MemoryShared<Value: Sendable>: Sendable { let subject: PassthroughSubject<T, Never> // ... var publisher: Publisher {} // ... } @propertyWrapper final class SharedState<Value> { var localSubject: Subject init(state: MemoryShared<Value>) { MainActor.assertIsolated() // Works! handle = localSubject.sink { MainActor.assertIsolated() // Fails } stateHandle = state.publisher.subscribe(localSubject) } } I’ve also tried: Using NotificationCenter Making the property wrapper a class Using NSKeyValueObserving Using a box class that is stored within the wrapper. Using @_inheritActorContext. All of these don’t work, because the event is never called from the thread the property wrapper resides in. Is it possible at all to create an observation system that notifies the observer from the same context as where the observer was created? Any help would be greatly appreciated!
2
0
385
Mar ’25
Lazy init of @State objects using new Observable protocol
Hi, Previously, we would conform model objects to the ObservableObject protocol and use the @StateObject property wrapper when storing them to an owned binding in a View. Now, if I understand correctly, it is recommended that we use the new @Observable macro/protocol in place of ObservableObject and use the @State property wrapper rather than @StateObject. This is my understanding from documentation articles such as Migrating from the Observable Object protocol to the Observable macro. However, the StateObject property wrapper has an initialiser which takes an autoclosure parameter: extension StateObject { public init(wrappedValue thunk: @autoclosure @escaping () -> ObjectType) } This is an extremely important initialiser for state objects that are expensive to allocate. As far as I can tell, the @State property wrapper lacks an equivalent initialiser. What is the recommended migration strategy for objects which made use of this on StateObject? Thanks
3
0
322
Mar ’25
SwiftUI @Observable Causes Extra Initializations When Using Reference Type Properties
I've encountered an issue where using @Observable in SwiftUI causes extra initializations and deinitializations when a reference type is included as a property inside a struct. Specifically, when I include a reference type (a simple class Empty {}) inside a struct (Test), DetailsViewModel is initialized and deinitialized twice instead of once. If I remove the reference type, the behavior is correct. This issue does not occur when using @StateObject instead of @Observable. Additionally, I've submitted a feedback report: FB16631081. Steps to Reproduce Run the provided SwiftUI sample code (tested on iOS 18.2 & iOS 18.3 using Xcode 16.2). Observe the console logs when navigating to DetailsView. Comment out var empty = Empty() in the Test struct. Run again and compare console logs. Change @Observable in DetailsViewModel to @StateObject and observe that the issue no longer occurs. Expected Behavior The DetailsViewModel should initialize once and deinitialize once, regardless of whether Test contains a reference type. Actual Behavior With var empty = Empty() present, DetailsViewModel initializes and deinitializes twice. However, if the reference type is removed, or when using @StateObject, the behavior is correct (one initialization, one deinitialization). Code Sample import SwiftUI enum Route { case details } @MainActor @Observable final class NavigationManager { var path = NavigationPath() } struct ContentView: View { @State private var navigationManager = NavigationManager() var body: some View { NavigationStack(path: $navigationManager.path) { HomeView() .environment(navigationManager) } } } final class Empty { } struct Test { var empty = Empty() // Comment this out to make it work } struct HomeView: View { private let test = Test() @Environment(NavigationManager.self) private var navigationManager var body: some View { Form { Button("Go To Details View") { navigationManager.path.append(Route.details) } } .navigationTitle("Home View") .navigationDestination(for: Route.self) { route in switch route { case .details: DetailsView() .environment(navigationManager) } } } } @MainActor @Observable final class DetailsViewModel { var fullScreenItem: Item? init() { print("DetailsViewModel Init") } deinit { print("DetailsViewModel Deinit") } } struct Item: Identifiable { let id = UUID() let value: Int } struct DetailsView: View { @State private var viewModel = DetailsViewModel() @Environment(NavigationManager.self) private var navigationManager var body: some View { ZStack { Color.green Button("Show Full Screen Cover") { viewModel.fullScreenItem = .init(value: 4) } } .navigationTitle("Details View") .fullScreenCover(item: $viewModel.fullScreenItem) { item in NavigationStack { FullScreenView(item: item) .navigationTitle("Full Screen Item: \(item.value)") .toolbar { ToolbarItem(placement: .cancellationAction) { Button("Cancel") { withAnimation(completionCriteria: .logicallyComplete) { viewModel.fullScreenItem = nil } completion: { var transaction = Transaction() transaction.disablesAnimations = true withTransaction(transaction) { navigationManager.path.removeLast() } } } } } } } } } struct FullScreenView: View { @Environment(\.dismiss) var dismiss let item: Item var body: some View { ZStack { Color.red Text("Full Screen View \(item.value)") .navigationTitle("Full Screen View") } } } Console Output With var empty = Empty() in Test DetailsViewModel Init DetailsViewModel Init DetailsViewModel Deinit DetailsViewModel Deinit Without var empty = Empty() in Test DetailsViewModel Init DetailsViewModel Deinit Using @StateObject Instead of @Observable DetailsViewModel Init DetailsViewModel Deinit Additional Notes This issue occurs only when using @Observable. Switching to @StateObject prevents it. This behavior suggests a possible issue with how SwiftUI handles reference-type properties inside structs when using @Observable. Using a struct-only approach (removing Empty class) avoids the issue, but that’s not always a practical solution. Questions for Discussion Is this expected behavior with @Observable? Could this be an unintended side effect of SwiftUI’s state management? Are there any recommended workarounds apart from switching to @StateObject? Would love to hear if anyone else has run into this or if Apple has provided any guidance!
0
0
266
Feb ’25
Listening Changes Out of swiftUI in Observation Framework
Hi, folks. I know that in the new observation, class property changes can be automatically notified to SwiftUI, which is very convenient. But in the new observation framework, how to monitor the property changes of different model classes? For example, class1 has an instance of class2, and I need to notify class1 to perform some actions and make some changes when some properties of class2 are changed. How to do it in observation? In the past, I could use combined methods to write the second part of the code for monitoring. However, using the combined framework in observation is a bit confusing. I know this method can be withObservationTracking(_:onChange:) but it needs to be registered continuously. If Observation is not possible, do I need to change my design structure? Thanks. // Observation @Observable class Sample1 { var count: Int = 0 var name = "Sample1" } @Observable class Sample2 { var count: Int = 0 var name = "Sample2" var sample1: Sample1? init (sample1 : Sample1) { self.sample1 = sample1 } func render() { withObservationTracking { print("Accessing Sample1.count: \(sample1?.count ?? 0)") } onChange: { [weak self] in print("Sample1.count changed! Re-rendering Sample2.") self?.handleSample1CountChange() } } private func handleSample1CountChange() { print("Handling count change in Sample2...") self.count = sample1?.count ?? 0 } } // ObservableObject class Sample1: ObservableObject { @Published var count: Int = 0 var name = "Sample1" } class Sample2: ObservableObject { @Published var count: Int = 0 var name = "Sample1" var sample1: Sample1? private var cancellables = Set<AnyCancellable>() init (sample1 : Sample1) { self.sample1 = sample1 setupSubscribers() } private func setupSubscribers() { sample1?.$count .receive(on: DispatchQueue.main) .sink { [weak self] count in guard let self = self else { return } // Update key theory data self.count = count self.doSomeThing() } .store(in: &cancellables) } private func doSomeThing() { print("Count changes, need do some thing") } }
1
0
286
Feb ’25
EXC_BREAKPOINT in BGAppRefreshTask
When I run my app with XCode on my iPhone, and then moved into the background, I'm getting a EXC_BREAKPOINT exception after a few minutes, seemingly when iOS attempts to call my app with a BGAppRefreshTask: Thread 23 Queue: com.apple.BGTaskScheduler (com.mycompany.MyApp.RefreshTask) (serial) 0 _dispatch_assert_queue_fail 12 _pthread_wqthread Enqueued from com.apple.duet.activityscheduler.client.xpcqueue (Thread 23) 0 dispatch_async 20 start_wqthread I can't quite understand the reason from this crash. In the background task, I'm attempting to update live activities. In the process, it might encounter code that calls MainActor and manipulate @Observable objects. Might that be the reason?
2
1
410
Feb ’25
Updated to @Observable, now the total not updated on each keystroke
Hi I am developing an app for counting money. One view is for entering the intended destinations specified by a donor. Each destination has a row with a TextField that has an OnChange to keep it all numeric. The model that holds the data is a class called AllocationItems, which I recently changed from having the protocol ObservableObject to having the macro @Observable. It mostly works the same, except that before with each key stroke the total would be updated, but now with the macro it only gets updated when I exit the current TextField by clicking on another TextField How do I get it to update the total with each keystroke? The code that shows the total is this: Text(allocations.totalAllocated.fmtCurrency) where allocations is an instance of AllocationItems with this definition: var totalAllocated: NSDecimalNumber { items.reduce(.zero) { $0 + $1.amountValue } } I hope someone knows why this has changed and can suggest a simple fix.
2
0
261
Feb ’25
Swift ObservableObject implementation with generic inference
I must admit my knowledge of swift is limited, and I cannot wrap my head around this problem. I've defined this protocol, so I can use different auth providers in my app. protocol AuthRepository { associatedtype AuthData associatedtype AuthResponseData associatedtype RegistrationData associatedtype RegistrationResponseData func login(with data: AuthData) async throws -&gt; AuthResponseData? func register(with data: RegistrationData) async throws -&gt; RegistrationResponseData? } and an implementation for my server struct MyServerAuthData { let email: String let password: String } struct MyServerAuthResponseData { let token: String } struct MyServerRegistrationData { let email: String let password: String let name: String } actor AuthRepositoryImpl: AuthRepository { func login(with data: MyServerAuthData) async throws -&gt; MyServerAuthResponseData? { ... } func register(with data: MyServerRegistrationData) async throws -&gt; Void? { ... } } To use across the app, I've created this ViewModel @MainActor final class AuthViewModel&lt;T: AuthRepository&gt;: ObservableObject { private let repository: T init(repository: T) { self.repository = repository } func login(data: T.AuthData) async throws -&gt; T.AuthResponseData? { try await repository.login(with: data) } func register(with data: T.RegistrationData) async throws { try await repository.register(with: data) } } defined in the app as @main struct MyApp: App { @StateObject var authViewModel = AuthViewModel(repository: AuthRepositoryImpl()) var body: some Scene { WindowGroup { ContentView() .environmentObject(self.authViewModel) } } } and consumed as @EnvironmentObject private var authViewModel: AuthViewModel&lt;AuthRepositoryImpl&gt; But with this code, the whole concept of having a generic implementation for the auth repository is useless, because changing the AuthRepostory will need to search and replace AuthViewModel&lt;AuthRepositoryImpl&gt; across all the app. I've experienced this directly creating a MockAuthImpl to use with #Preview, and the preview crashed because it defines AuthViewModel(repository: MockAuthImpl()) but the view expects AuthViewModel. There is a better way to do that?
1
0
255
Feb ’25
How to initialize @Observable object in View via @State?
When I switched to observable, I noticed a strange behavior of the ViewModel. The ViewModel is created 3x times. And my question is: How to properly initialize the ViewModel via state? Below is a minimal example with log output: ViewModel INIT : EBBB2C41 ViewModel INIT : D8E490DA ViewModel INIT : 54407300 ViewModel DEINIT: D8E490DA @Observable final class ViewModel { @ObservationIgnored let idd: UUID init() { idd = UUID() print("ViewModel INIT : \(idd.uuidString.prefix(8))") } deinit { print("ViewModel DEINIT: \(idd.uuidString.prefix(8))") } } struct SimpleView: View { @Environment(ViewModel.self) private var viewModel var body: some View { @Bindable var viewModel = viewModel Text("SimpleView: \(viewModel.idd.uuidString.prefix(8))") } } struct ContentView: View { @State private var viewModel = ViewModel() var body: some View { SimpleView() .environment(mainViewModel) } }
1
0
370
Feb ’25
ObservableObject With Closure With Typed Throws Crashes on iOS 17
Having a property inside of an ObservableObject with a type of a closure with a typed throws will crash the app on the initialization of the observable object on iOS 17. Here is an example: struct ContentView: View { @StateObject var myDataSource = MyDataSource() var body: some View { EmptyView() } } enum MyError: Error { case error } class MyDataSource: ObservableObject { let signUp: (Int) throws(MyError) -> Void = { _ in } } If you run this code on iOS 17, the app will crash. The Radar for this issue is FB16399987.
1
0
363
Jan ’25
swiftdata model polymorphism?
I have a SwiftData model where I need to customize behavior based on the value of a property (connectorType). Here’s a simplified version of my model: @Model public final class ConnectorModel { public var connectorType: String ... func doSomethingDifferentForEveryConnectorType() { ... } } I’d like to implement doSomethingDifferentForEveryConnectorType in a way that allows the behavior to vary depending on connectorType, and I want to follow best practices for scalability and maintainability. I’ve come up with three potential solutions, each with pros and cons, and I’d love to hear your thoughts on which one makes the most sense or if there’s a better approach: **Option 1: Use switch Statements ** func doSomethingDifferentForEveryConnectorType() { switch connectorType { case "HTTP": // HTTP-specific logic case "WebSocket": // WebSocket-specific logic default: // Fallback logic } } Pros: Simple to implement and keeps the SwiftData model observable by SwiftUI without any additional wrapping. Cons: If more behaviors or methods are added, the code could become messy and harder to maintain. **Option 2: Use a Wrapper with Inheritance around swiftdata model ** @Observable class ParentConnector { var connectorModel: ConnectorModel init(connectorModel: ConnectorModel) { self.connectorModel = connectorModel } func doSomethingDifferentForEveryConnectorType() { fatalError("Not implemented") } } @Observable class HTTPConnector: ParentConnector { override func doSomethingDifferentForEveryConnectorType() { // HTTP-specific logic } } Pros: Logic for each connector type is cleanly organized in subclasses, making it easy to extend and maintain. Cons: Requires introducing additional observable classes, which could add unnecessary complexity. **Option 3: Use a @Transient class that customizes behavior ** protocol ConnectorProtocol { func doSomethingDifferentForEveryConnectorType(connectorModel: ConnectorModel) } class HTTPConnectorImplementation: ConnectorProtocol { func doSomethingDifferentForEveryConnectorType(connectorModel: ConnectorModel) { // HTTP-specific logic } } Then add this to the model: @Model public final class ConnectorModel { public var connectorType: String @Transient public var connectorImplementation: ConnectorProtocol? // Or alternatively from swiftui I could call myModel.connectorImplementation.doSomethingDifferentForEveryConnectorType() to avoid this wrapper func doSomethingDifferentForEveryConnectorType() { connectorImplementation?.doSomethingDifferentForEveryConnectorType(connectorModel: self) } } Pros: Decouples model logic from connector-specific behavior. Avoids creating additional observable classes and allows for easy extension. Cons: Requires explicitly passing the model to the protocol implementation, and setup for determining the correct implementation needs to be handled elsewhere. My Questions Which approach aligns best with SwiftData and SwiftUI best practices, especially for scalable and maintainable apps? Are there better alternatives that I haven’t considered? If Option 3 (protocol with dependency injection) is preferred, what’s the best way to a)manage the transient property 2) set the correct implementation and 3) pass reference to swiftdata model? Thanks in advance for your advice!
0
0
428
Jan ’25
Type 'AVPlayer.Type' cannot conform to 'ObservableObject'
I'm having the following issue: Type 'AVPlayer.Type' cannot conform to 'ObservableObject' struct MusicEditorView: View { @ObservedObject var audioPlayer = AVPlayer and this is the class: class MusicPlayer: ObservableObject { private var audioPlayer: AVPlayer? private var timer: Timer? func playSound(named sFileName: String){ if let url = Bundle.main.url(forResource: sFileName, withExtension: "mp3"){ audioPlayer = try? AVPlayer(url: url) audioPlayer?.play() } } func pause(){ audioPlayer?.pause() } func getcurrentProgress() -&gt; Double{ guard let currentTime = audioPlayer?.currentItem?.currentTime().seconds else { return 0 } guard let duration = audioPlayer?.currentItem?.duration.seconds else { return 0 } return duration &gt; 0 ? (currentTime / duration) * 100 : 0 } func startProgressTimer(updateProgress: @escaping (Double, Double) -&gt; Void){ timer?.invalidate() timer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { _ in guard let currentTime = self.audioPlayer?.currentItem?.currentTime().seconds else { return } guard let duration = self.audioPlayer?.currentItem?.duration.seconds else { return } updateProgress(currentTime, duration) } } func stopProgressTimer(){ timer?.invalidate() } struct Sound: Identifiable, Codable { var id = UUID() var name: String var fileName: String } } }
1
0
380
Jan ’25
Why is ScrollView / LazyVStack retaining Views which causes memory leaks in the end?
Recently I noticed how my ViewModels aren't deallocating and they end up as a memory leaks. I found something similar in this thread but this is also happening without using @Observation. Check the source code below: class CellViewModel: Identifiable { let id = UUID() var color: Color = Color.red init() { print("init") } deinit { print("deinit") } } struct CellView: View { let viewModel: CellViewModel var body: some View { ZStack { Color(viewModel.color) Text(viewModel.id.uuidString) } } } @main struct LeakApp: App { @State var list = [CellViewModel]() var body: some Scene { WindowGroup { Button("Add") { list.append(CellViewModel()) } Button("Remove") { list = list.dropLast() } ScrollView { LazyVStack { ForEach(list) { model in CellView(viewModel: model) } } } } } } When I tap the Add button twice in the console I will see "init" message twice. So far so good. But then I click the Remove button twice and I don't see any "deinit" messages. I used the Debug Memory Graph in Xcode and it showed me that two CellViewModel objects are in the memory and they are owned by the CellView and some other objects that I don't know where are they coming from (I assume from SwiftUI internally). I tried using VStack instead of LazyVStack and that did worked a bit better but still not 100% "deinits" were in the Console. I tried using weak var struct CellView: View { weak var viewModel: CellViewModel? .... } but this also helped only partially. The only way to fully fix this is to have a separate class that holds the list of items and to use weak var viewModel: CellViewModel?. Something like this: class CellViewModel: Identifiable { let id = UUID() var color: Color = Color.red init() { print("init") } deinit { print("deinit") } } struct CellView: View { var viewModel: CellViewModel? var body: some View { ZStack { if let viewModel = viewModel { Color(viewModel.color) Text(viewModel.id.uuidString) } } } } @Observable class ListViewModel { var list = [CellViewModel]() func insert() { list.append(CellViewModel()) } func drop() { list = list.dropLast() } } @main struct LeakApp: App { @State var viewModel = ListViewModel() var body: some Scene { WindowGroup { Button("Add") { viewModel.insert() } Button("Remove") { viewModel.drop() } ScrollView { LazyVStack { ForEach(viewModel.list) { model in CellView(viewModel: model) } } } } } } But this won't work if I want to use @Bindable such as @Bindable var viewModel: CellViewModel? I don't understand why SwiftUI doesn't want to release the objects?
0
1
444
Dec ’24
Replacing array in Observed object won't update UI.
Hello, following is the issue: I have a @Observable view model which has an array of @Observable Items. Tapping an item leads to a detail like view to submit a value to the item. This work thru bindings. However I have the need to replace the contents of the array entirely with a fresh version loaded from the network. It will contain the "same" objects with the same id but some values might have changed. So replacing the entire array seems to not update the UI because the IDs are the same as before. Also this seems to break the bindings because when replacing the array, editing no longer updates the UI. How to test the behavior: Launch the app in simulator. Add some values to the items by tapping on an item and then on add. Notice how changes are updated. Tap the blue button to sync fresh data to the array. (Not replacing the actual array) Confirm everything is still working Replace the array with the red button. Editing and UI updates are broken from now on. What is the proper way to handle this scenario? Project: https://github.com/ChristianSchuster/DTS_DataReplaceExample.git
2
0
973
Dec ’24