Observation

RSS for tag

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

Posts under Observation tag

55 Posts
Sort by:

Post

Replies

Boosts

Views

Activity

@Transient update doesn't propagate to view
When I update a variable inside my model that is marked @Transient, my view does not update with this change. Is this normal? If I update a non-transient variable inside the model at the same time that I update the transient one, then both changes are propagated to my view. Here is an example of the model: @Model public class WaterData { public var target: Double = 3000 @Transient public var samples: [HKQuantitySample] = [] } Updating samples only does not propagate to my view.
6
6
1.6k
Sep ’23
Observation and MainActor
Previously, it was recommended to use the @MainActor annotation for ObservableObject implementation. @MainActor final class MyModel: ObservableObject { let session: URLSession @Published var someText = "" init(session: URLSession) { self.session = session } } We could use this as either a @StateObject or @ObservedObject: struct MyView: View { @StateObject let model = MyModel(session: .shared) } By moving to Observation, I need to the @Observable macro, remove the @Published property wrappers and Switch @StateObject to @State: @MainActor @Observable final class MyModel { let session: URLSession var someText = "" init(session: URLSession) { self.session = session } } But switching from @StateObject to @State triggers me an error due to a call to main-actor isolated initialiser in a synchronous nonisolated context. This was not the case with @StateObject of @ObservedObject. To suppress the warning I could : mark the initializer as nonisolated but it is not actually what I want Mark the View with @MainActor but this sounds odd Both solutions does not sound nice to my eye. Did I miss something here?
9
3
4.3k
Apr ’24
Mix new @Observable with MVVM and Combine
We currently have our entire app written as SwiftUI Views with ViewModels (currently set as @StateObjects). SwiftUI has a new feature in iOS 17 called @Observable which simplifies the MVVM pattern and would greatly reduce the complexity of our codebase. However, our current ViewModels implement Combine pipelines on the @Published properties which allows us to do all sorts of things from validation of inputs to ensuring lists are filtered correctly. Without the @Published property wrapper in the new @Observable macro, we don't have access to those combine pipelines and so we were wondering how others have solved this? One idea we are floating around is using CurrentValueSubjects as the variable types, but that does pollute the code a little as we have to utilise .send() and .value in the Views which seems like an anti-pattern. Any thoughts or help would be greatly appreciated!
2
3
1.4k
Aug ’23
@ObservationIgnored weak var - must be initialised?
I understand that @Observable currently requires all properties to be initialised. However I am surprised that opting out of observation does not work: @Observable class Assessment { var name = "" var processes: [Process] = [] } @Observable class Process { var name = "" @ObservationIgnored weak var parent: Assessment? } The error message is Return from initializer without initializing all stored properties from the Process class. Any suggestions?
0
1
839
Jul ’23
Trouble with Persisting One-to-Many Relationship Data in SwiftData, What am I Missing?
Hi everyone, I'm new to programming and I've been experimenting with Apple's SwiftData. I've run into an issue I can't seem to resolve. I'm creating a personal relationship manager app where I have a Relation model and an Interaction model. Relation has a one-to-many relationship with Interaction. I'm using SwiftData's @Model and @Relationship property wrappers to define these models and their relationship. I've taken inspiration from Apple's sample code, that can be found here: Adopting SwiftData for a Core Data app (WWDC23 Session: "Migrate to SwiftData") The relevant parts of the models look something like this: @Model final class Relation { ... @Relationship(.cascade, inverse: \Interaction.relation) var interactions: [Interaction] = [] ... } @Model final class Interaction { ... var relation: Relation? ... } In my SwiftUI view, I'm adding a new Interaction to a Relation like this: private func AddItem() { withAnimation { let newInteraction = Interaction(...) modelContext.insert(newInteraction) newInteraction.relation = relation relation.interactions.append(newInteraction) } } When I add a new Interaction like this, everything seems to work fine during that app session. I can see the new Interaction in my app's UI. But when I quit the app and relaunch it, the new Interaction is gone. It's as if it was never saved. I've double-checked my code and as far as I can tell, I'm using SwiftData correctly. My usage aligns with the sample code provided by Apple, and I'm not getting any errors or warnings. I think that this issue is not related to SwiftData being in Beta, because Apple's sample code works perfectly fine. I have a few questions: Is there something I'm missing about how to properly save models using SwiftData? Is there a specific step or method I need to call to persist the changes to the Relation and Interaction objects? Is there a way to debug what's going wrong when SwiftData attempts to save these changes? Any help would be greatly appreciated. Thank you in advance! Louis
2
0
1.7k
Aug ’23
NavigationPath doesn't work with the @Observable macro iOS 17
Hello! I'm not able to push a view into a stack using new @Observable macro. import SwiftUI import Observation @Observable class NavigationModel { var path = NavigationPath() } struct ContentView: View { @State var navigationModel: NavigationModel = NavigationModel() var body: some View { NavigationStack(path: $navigationModel.path) { VStack { Button { navigationModel.path.append("Text") } label: { Text("Go to the next screen") } } .navigationDestination(for: String.self) { item in Text("Pushed view") } } } } Everything works fine when I use ObservableObject with @Published properties: class NavigationModel: ObservableObject { @Published var path = NavigationPath() } struct ContentView: View { @StateObject var navigationModel: NavigationModel = NavigationModel() var body: some View { NavigationStack(path: $navigationModel.path) { Button { navigationModel.path.append("Text") } label: { Text("Go to the next screen") } .navigationDestination(for: String.self) { item in Text("Pushed view") } } } }
1
0
1.2k
Jul ’23
Swift compiler crash in Xcode 15 beta 3 (15A5195k)
Somehow the Swift compiler is unable to this code: @Observable class ServiceBrowser: NSObject { typealias ResolveServiceCompletionBlock = (Bool, Error?) -> Void fileprivate var resolveServiceCompletionHandler: ResolveServiceCompletionBlock? = nil } Here's the crash: 4 . While evaluating request ASTLoweringRequest(Lowering AST to SIL for file "/Users/luc/Work/Repositories/app-shared/App/Shared/Connectivity/ServiceBrowser.swift") 5 . While silgen init accessor SIL function "@$s7App14ServiceBrowserC07resolveB17CompletionHandler33_5B15C352D9CC926D1F8A0ECAC5970199LLySb_s5Error_pSgtcSgvi". for init for resolveServiceCompletionHandler (at /Users/luc/Work/Repositories/ap-shared/App/Shared/Connectivity/ServiceBrowser.swift:86:21) 6 . While emitting reabstraction thunk in SIL function "@$sSbs5Error_pSgIegyg_ytIegd_TR".
2
0
947
Jul ’23
@Observable does not conform with Equatable (and Hashable)
Since Xcode 15 beta 5, making a class with the @Observable macro no longer requires all properties to have an initialization value, as seen in the video. Just put an init that collects the properties and everything works correctly. @Observable final class Score: Identifiable { let id: Int var title: String var composer: String var year: Int var length: Int var cover: String var tracks: [String] init(id: Int, title: String, composer: String, year: Int, length: Int, cover: String, tracks: [String]) { self.id = id self.title = title self.composer = composer self.year = year self.length = length self.cover = cover self.tracks = tracks } } But there is a problem: the @Observable macro makes each property to integrate the @ObservationTracked macro that seems not to conform the types to Equatable, and in addition, to Hashable. Obviously, being a feature of each property, it is not useful to conform the class in a forced way with the static func == or with the hash(into:Hasher) function that conforms both protocols. That any class we want to be @Observable does not conform to Hashable, prevents any instance with the new pattern to be usable within a NavigationStack using the data driven navigation bindings and the navigationDestination(for:) modifier. I understand that no one has found a solution to this. If you have found it it would be great if you could share it but mainly I am making this post to invoke the mighty developers at Apple to fix this bug. Thank you very much. P.S. - I also posted a Feedback (FB12535713), but no one replies. At least that I see.
3
4
1.4k
Apr ’24
Use multiple @Observable inside each other using @Enviroment
Hello I'm trying the new Observation in SwiftUI, I created 2 classes using the new @Observable wrapper like this: @Observable class CategoriesViewModel { var categories: [Category] = [] } @Observable class NotesViewModel { var notes: [Note] = [] } and I'm using them in the views like this: @Environment(CategoriesViewModel.self) var categoriesViewModel The question is: what if I want to use an observable inside another observable ? for example, I want to use the categories inside the NotesViewModel, I tried the @State instead of @Enviroment but it's not a global across all classes, so I want to use it as an environment variable like what I did in the views, I tried to use it inside the Observable class like this but I got an error below: @Observable class NotesViewModel { var notes: [Note] = [] @ObservationIgnored @Environment(CategoriesViewModel.self) var categoriesViewModel: CategoriesViewModel func foo(){ /// Thread 1: Fatal error: No Observable object of type CategoriesViewModel found. A View.environmentObject(_:) for CategoriesViewModel may be missing as an ancestor of this view. print(categoriesViewModel.categories.count) } }
1
0
810
Aug ’23
[SwiftData] Bugs with `autosaveEnabled` and `undoManager` + Observation
Hello, my goal is it to implement an Edit Sheet of a Model with Discard changes in SwiftData. Xcode Version 15.0 beta 6 (15A5219j) Approaches In the following modelContext refers to the context fetched from the environment by the View. Also code for showing the sheet (eg. toggling isPresented) is omitted. // App ContentView() .modelContainer(for: Model.self, isAutosaveEnabled: true, isUndoEnabled: true) // In Content View @Query var models: [Model] //... ForEach(models) { model in ModelView(model: model) //eg. longpresgesture that calls openEditSheet .sheet(..., content: { EditModelView(model: model) } } Disabling autosave + rollback // In ContentView func openEditSheet() { modelContext.autosaveEnabled = false } // In EditModelView func discardEditSheet() { modelContext.rollback() modelContext.autosaveEnabled = true } func saveEditSheet() { try? modelContext.save() // probably not needed if autosave gets enabled anyway modelContext.autosaveEnabled = true } However this approach does not work, as SwiftData continues to save anyway and therefore there is nothing to rollback. modelContext in Memory // In ContentView let context = ModelContext(modelContext.container) context.autosaveEnabled = false context.container.configuration.removeAll() context.container.configuration.insert(ModelConfiguration(..., inMemory: true) //... EditModelView() .modelContext(context) // Also tried: .enviroment(\.modelContext, context) // In EditModelView func discardEditSheet() { modelContext.rollback() // probably not needed as the container is never saved } func saveEditSheet() { try? modelContext.save() // move to persistent storage from memory } The idea was to use something like parent in CoreData. However as this is (currently) not supported. Also tried this by modifying the modelContext directly (instead of creating a new context) and its container directly or before creating the context. The child can not be a ModelContainer as you can not pass a context to it or set its mainContext (get-only). However this approach does not work, as the container does not seem to stay in memory. Maybe related to previous. BackingData EditModelView(model: Model(backingData: model.persistentBackingData) not sure about this one, played around a little with it but not sure what it means / should mean and how I would expect it to work. I have never been great at cooking :). UndoManager (preferred) // In ContentView func openEditSheet() { modelContext.undoManager?.beginUndoGrouping() } // In EditModelView func discardEditSheet() { modelContext.undoManager?.endUndoGrouping() modelContext.undoManager?.undoNestedGroup() // Also tried adding: try? modelContext.save() } func saveEditSheet() { modelContext.undoManager?.endUndoGrouping() } This approach does works kinda weird: The ModelView of the updated model in ContentView's ForEach is not updated regarding the undo action. However when I re-open the EditTaskView of the updated model it has the expected state, even though the model to edit is passed by the ContentView (which does not have the correct state?). After relaunching the app or when modifying the model again (this time not undoing) the previous sate changes are recognised Therefore this looks like it does what I want, with the ContentView having the correct state, but not displaying it. Only reason I can think of is the Model: Observable not getting triggered and therefore no View update. Conclusion After playing around with the above approaches and combining them in every possible way, i think that: disabling autosave at runtime is currently not working and this is a bug (otherwise autosaveEnabled should be get-only). UndoManager does not trigger a View update of a Model/Observable and this is a bug Would be happy to hear other opinions on this. Am I missing something here? Am I ******? Question However as my conclusion does not fix my problem I am wondering: Are my approaches (theoretically) correct? How do I fix / workaround the UndoManager issue? If I identified it correctly, how can I manually notify the view that a Observable model has changed?
3
3
1.3k
Nov ’23
iOS 17b6: Simultaneous accesses to ..., but modification requires exclusive access crash using Observation and SwiftUI
Since iOS/iPadOS beta 6, I get a crash just by simply selecting an item in the sidebar of a navigation view. The selected item is part of an app mode, with conforms to the Observation protocol: import SwiftUI import Observation @main struct MREAApp: App { @State private var appModel = AppModel.shared var body: some Scene { WindowGroup { ContentView() .environment(appModel) } } } @Observable class AppModel { static var shared = AppModel() var selectedFolder: Folder? = nil } struct Folder: Hashable, Identifiable { let id = UUID() let name: String } struct ContentView: View { @Environment(AppModel.self) private var appModel var folders = [Folder(name: "Recents"), Folder(name: "Deleted"), Folder(name: "Custom")] var body: some View { NavigationSplitView { SidebarView(appModel: appModel, folders: folders) } detail: { if let folder = appModel.selectedFolder { Text(folder.name) } else { Text("No selection") } } } } struct SidebarView: View { @Bindable var appModel: AppModel var folders: [Folder] var body: some View { List(selection: $appModel.selectedFolder) { ForEach(folders, id: \.self) { folder in NavigationLink(value: folder) { Text(folder.name) } } } } } To reproduce the bug, just tap on one of the item. Oddly enough, this works fine in the Simulator. macOS 14 beta 5 is not affected either. Apple folks: FB12981860
16
2
2.6k
Oct ’23
@Observable/@State memory leak
Not sure if this code is supposed to leak memory or am I missing something? import SwiftUI import Observation @Observable class SheetViewModel { let color: Color = Color.red init() { print("init") } deinit { print("deinit") } } struct SheetView: View { @State var viewModel = SheetViewModel() var body: some View { Color(viewModel.color) } } @main struct ObservableMemoryLeakApp: App { @State var presented: Bool = false var body: some Scene { WindowGroup { Button("Show") { presented = true } .sheet(isPresented: $presented) { SheetView() } } } } Every time the sheet is presented/dismissed, a new SheetViewModel is created (init is printed) but it never released (deinit not printed). Also, all previously created SheetViewModel instances are visible in Memory Graph and have "Leaked allocation" badge. Reverting to ObservableObject/@StateObject fixes the issue, deinit is called every time the sheet is dismissed: -import Observation -@Observable class SheetViewModel { +class SheetViewModel: ObservableObject { - @State var viewModel = SheetViewModel() + @StateObject var viewModel = SheetViewModel() Does this mean there's a bug in Observation framework? Reported as FB13015569.
7
10
2.9k
Nov ’23
@Observable not working in Xcode playgrounds
Creating an Observable class in an Xcode playground seems to cause an error. This stops me from being able to run the playground. As a demo, try creating a playground page and add: import Foundation import Observation // Doesn't seem to make a difference whether it's added or not. @Observable public class NewViewModel: Observable { var value: Int = 1 func increment() { value += 1 } } Tapping the run button logs the following error (and then it builds successfully, buy fails silently): error: Untitled Page.xcplaygroundpage:7:9: error: expansion of macro 'ObservationTracked()' did not produce a non-observing accessor var value: Int = 1 ^ Putting the cursor on @Observable and then selecting Editor > Expand Macro shows that value has been annotated with @ObservationTracked and I if I re-run the playground I can even see the @ObservationTracked generated code. Macros: import Foundation import Observation // Doesn't seem to make a difference whether it's added or not. @Observable public class NewViewModel: Observable { @ObservationTracked // original-source-range: /Users/gabriel.banfalvi/work/forums_observation/ObservationFramework.playground/Pages/Untitled Page.xcplaygroundpage:7:5-7:5 var value: Int = 1 { @storageRestrictions(initializes: _value) init(initialValue) { _value = initialValue } get { access(keyPath: \.value) return _value } set { withMutation(keyPath: \.value) { _value = newValue } } } // original-source-range: /Users/gabriel.banfalvi/work/forums_observation/ObservationFramework.playground/Pages/Untitled Page.xcplaygroundpage:7:20-7:23 func increment() { value += 1 } @ObservationIgnored private let _$observationRegistrar = Observation.ObservationRegistrar() internal nonisolated func access<Member>( keyPath: KeyPath<NewViewModel, Member> ) { _$observationRegistrar.access(self, keyPath: keyPath) } internal nonisolated func withMutation<Member, MutationResult>( keyPath: KeyPath<NewViewModel, Member>, _ mutation: () throws -> MutationResult ) rethrows -> MutationResult { try _$observationRegistrar.withMutation(of: self, keyPath: keyPath, mutation) } @ObservationIgnored private var _value: Int = 1 // original-source-range: /Users/gabriel.banfalvi/work/forums_observation/ObservationFramework.playground/Pages/Untitled Page.xcplaygroundpage:12:1-12:1 } I can't seem to be able to expand the @ObservationIgnored macros, which may or may not be related to the issue. I can't expand them in a regular Xcode app, but it doesn't lead to any problems there. Im running an iOS playground. I get this in Xcodes 15 beta 6 and 5. I get a different error in earlier versions.
1
0
951
Aug ’23
NavigationSplitView crashes in Xcode, iOS Beta 7
I have a NavigationSplitView with a sidebar. When selecting a new item on the sidebar, the app crashes. The error message says: Simultaneous accesses to 0x6000030107f0, but modification requires exclusive access. Xcode shows that the crash occurs inside the generated code in my class with @Observable macro. @ObservationIgnored private let _$observationRegistrar = Observation.ObservationRegistrar() internal nonisolated func access<Member>( keyPath: KeyPath<NavModel , Member> ) { _$observationRegistrar.access(self, keyPath: keyPath) } internal nonisolated func withMutation<Member, MutationResult>( keyPath: KeyPath<NavModel , Member>, _ mutation: () throws -> MutationResult ) rethrows -> MutationResult { // Crash occurs on the following line try _$observationRegistrar.withMutation(of: self, keyPath: keyPath, mutation) } @ObservationIgnored private var _section: SidebarSection? = .one To reproduce the crash, I tap a new item on the sidebar until the app crashes. It usually only takes 1-3 times selecting a new item before the crash occurs. Below is the code for an entire app to reproduce the crash. Has anyone else encountered this issue? Thank you! import SwiftUI @main struct NavigationBugApp: App { var body: some Scene { WindowGroup { ContentView() } } } @Observable class NavModel { var section: SidebarSection? = .one } enum SidebarSection: Hashable { case one case two } struct ContentView: View { @State private var model = NavModel() var body: some View { NavigationSplitView { List(selection: $model.section) { NavigationLink("One", value: SidebarSection.one) NavigationLink("Two", value: SidebarSection.two) } .listStyle(.sidebar) } detail: { Text("Hello World") } } } #Preview { ContentView() }
2
0
838
Aug ’23
@State ViewModel memory leak in iOS 17 (new Observable)
Our app has an architecture based on ViewModels. Currently, we are working on migrating from the ObservableObject protocol to the Observable macro (iOS 17+). The official docs about this are available here: https://developer.apple.com/documentation/swiftui/migrating-from-the-observable-object-protocol-to-the-observable-macro Our ViewModels that were previously annotated with @StateObject now use just @State, as recommended in the official docs. Some of our screens (a screen is a SwiftUI view with a corresponding ViewModel) are presented modally. We expect that after dismissing a SwiftUI view that was presented modally, its corresponding ViewModel, which is owned by this view (via the @State modifier), will be deinitialized. However, it seems there is a memory leak, as the ViewModel is not deinitialized after a modal view is dismissed. Here's a simple code where ModalView is presented modally (through the .sheet modifier), and ModalViewModel, which is a @State of ModalView, is never deinitialized. import SwiftUI import Observation @Observable final class ModalViewModel { init() { print("Simple ViewModel Inited") } deinit { print("Simple ViewModel Deinited") // never called } } struct ModalView: View { @State var viewModel: ModalViewModel = ModalViewModel() let closeButtonClosure: () -> Void var body: some View { ZStack { Color.yellow .ignoresSafeArea() Button("Close") { closeButtonClosure() } } } } struct ContentView: View { @State var presentSheet: Bool = false var body: some View { Button("Present sheet modally") { self.presentSheet = true } .sheet(isPresented: $presentSheet) { ModalView { self.presentSheet = false } } } } #Preview { ContentView() } Is this a bug in the iOS 17 beta version or intended behavior? Is it possible to build a relationship between the View and ViewModel in a way where the ViewModel will be deinitialized after the View is dismissed? Thank you in advance for the help.
3
12
2.1k
Oct ’23
SwiftData - What is Best Practice for returning an object from a sheet
SwiftUI & SwiftData. I have a view that lists SwiftData objects. Tapping on a list item navigates to a detail view. The list view also has a "New Object" button. Tapping it opens a sheet used to create a new object. There are, obviously, two possible outcomes from interacting with the sheet — a new object could be created or the user could cancel without creating a new object. If the user creates a new object using the sheet, I want to open the detail view for that object. My thought was to do this in the onDismiss handler for the sheet. However, that handler takes no arguments. What is best practice for handling this situation? In UIKit, I would return a Result<Object, Error> in a closure. That won't work here. What is the "correct" way to handle this that is compatible with SwiftData and the Observation framework?
2
0
1.3k
Aug ’23
SwiftUI - Using Bindable with Environment
At the moment, using Bindable for an object stored in Environment works in a cumbersome way: struct ContentView: View { @Environment(Model.self) var model var body: some View { @Bindable var model = model VStack { Text(model.someField.uppercased()) TextField("", text: $model.someField) someSubView } .padding() } @ViewBuilder var someSubView: some View { @Bindable var model = model TextField("", text: $model.someField) } } A new @Bindable needs to be instantiated for each computed property in the view, which creates boilerplate I would like to avoid. I made a new property wrapper which functions the same as the EnvironmentObject wrapper, but for Observable: @propertyWrapper struct EnvironmentObservable<Value: AnyObject & Observable>: DynamicProperty { @Environment var wrappedValue: Value public init(_ objectType: Value.Type) { _wrappedValue = .init(objectType) } public init() { _wrappedValue = .init(Value.self) } private var store: Bindable<Value>! var projectedValue: Bindable<Value> { store } mutating func update() { store = Bindable(wrappedValue) } } Example: struct ContentView: View { @EnvironmentObservable var model: Model var body: some View { VStack { Text(model.someField.uppercased()) SubView(value: $model.someField) someSubView } .padding() } var someSubView: some View { TextField("", text: $model.someField) } } I was wondering if there would be any downsides to using this method? In my testings it seems to behave the same, but I'm not sure if using this could have a performance impact.
1
2
549
3w