Warning when using @Perceptible with nested observable objects #37
Replies: 4 comments 3 replies
-
Hi @kentanakae, I'm not able to reproduce this on my side. Can you please provide a minimal project that demonstrates the behavior you are seeing? |
Beta Was this translation helpful? Give feedback.
-
Hi @mbrandonw, Thank you for your response. Here is the sample code that demonstrates the issue: import SwiftUI
import Perception
//@Observable
@Perceptible
class Book: Identifiable {
var title: String
let id = UUID()
init(title: String) {
self.title = title
}
}
//@Observable
@Perceptible
class Library {
var books: [Book] = [
Book(title: "Sample Book 1"),
Book(title: "Sample Book 2")
]
func addBook() {
books.append(Book(title: "Sample Book \(books.count + 1)"))
}
func updateFirstBook() {
guard !books.isEmpty else { return }
books[0].title = "Updated Title"
}
}
struct BookView: View {
var book: Book
var body: some View {
WithPerceptionTracking {
VStack {
Text(book.title).font(.headline)
}
}
}
}
struct ContentView: View {
var library: Library
var body: some View {
WithPerceptionTracking {
NavigationView {
List(library.books) { book in
NavigationLink(destination: BookView(book: book)) {
Text(book.title)
}
}
.navigationTitle("Library")
.toolbar {
ToolbarItemGroup(placement: .navigationBarTrailing) {
Button("Add Book") {
library.addBook()
}
Button("Update First Book") {
library.updateFirstBook()
}
}
}
}
}
}
} Please let me know if there's any additional information I can provide to help resolve this issue. |
Beta Was this translation helpful? Give feedback.
-
The issue you're facing here is documented https://pointfreeco.github.io/swift-composable-architecture/main/documentation/composablearchitecture/observationbackport#Lazy-view-closures. |
Beta Was this translation helpful? Give feedback.
-
Hi @kentanakae, as @Brett-Best pointed out, this is expected. You need to wrap the inside of the Since this isn't an issue with the library I am going to convert it to a discussion. |
Beta Was this translation helpful? Give feedback.
-
Description
When using the
@Perceptible
property wrapper from the Perception library in a nested observable object scenario, a warning is displayed that states the perceptible state was accessed but is not being tracked, despite the view being wrapped in aWithPerceptionTracking
view. This behavior occurs when modifying the list ofBook
objects contained within aLibrary
object. Notably, despite the appearance of this warning, the application continues to function as expected, with the UI correctly updating to reflect changes. This suggests that while the warning is concerning, it does not appear to critically affect the app's functionality. Previously, using@Observable
did not result in this warning, suggesting the issue is specific to the@Perceptible
implementation.Checklist
@Observable
macro or another tool from theObservation
framework, please file it directly with Apple.main
branch of this package.Expected behavior
I expected that using
@Perceptible
in the same manner as@Observable
would not produce any warnings when the state changes are tracked within aWithPerceptionTracking
view. Specifically, modifications to theLibrary
object's books array should be tracked without issue, allowing for a seamless update of the UI.Actual behavior
Upon modifying the
Library
object (e.g., adding a newBook
to thebooks
array), the console displays the following warning:This occurs despite the views being properly wrapped with
WithPerceptionTracking
, indicating an issue with how nested@Perceptible
objects are tracked or a misunderstanding of the correct implementation.It is important to note that despite this warning, the application continues to function as expected, with the UI updating correctly to reflect changes to the
Library
object. This suggests that the warning may not indicate a critical failure in state tracking, but it is still concerning and potentially indicative of an underlying issue that could affect future stability or performance.Steps to reproduce
@Perceptible
.Library
contains an array ofBook
instances.Library
object, ensuring the view is wrapped inWithPerceptionTracking
.Library
object by adding a newBook
or changing properties of existingBook
instances.Below is a simplified code snippet demonstrating the issue:
This structure results in the aforementioned warning when the
Library
object is modified, despite expectations based on the library's documentation and comparable use of@Observable
.Perception version information
1.1.1
Destination operating system
iOS/iPadOS 15, 16
Xcode version information
Xcode 15.2
Swift Compiler version information
Beta Was this translation helpful? Give feedback.
All reactions