Is @FetchOne supposed to be reactive when used inside a view body? #331
Answered
by
mbrandonw
SublimeAbode
asked this question in
Q&A
-
|
It seems that the @FetchOne property wrapper is not reactive when used inside a view body. Is this to be expected? Consider the following code where the @FetchOne property does not behave reactively: // Does not work
struct TaskDetailViewWrapper: View {
let taskID: Task.ID
var body: some View {
@FetchOne(Task.find(taskID)) var task
if let task {
TaskDetailView(task: task)
// The Task record is later mutated from the detail view, but TaskDetailView does not re-render with the updated value.
}
}
}And now a modified example where it does: // Works
struct TaskDetailViewWrapper: View {
let taskID: Task.ID
@FetchOne var task: Task? // Move the wrapper up to the view definition
init(taskID: Task.ID) {
self.taskID = taskID
self._task = FetchOne(wrappedValue: nil, Task.find(taskID)) // Update the wrapper in the initializer
}
var body: some View {
if let task {
TaskDetailView(task: task)
// The Task record is later mutated from the detail view, and TaskDetailView DOES re-render with the updated value.
}
}
} |
Beta Was this translation helpful? Give feedback.
Answered by
mbrandonw
Dec 13, 2025
Replies: 1 comment 1 reply
-
|
Hi @SublimeAbode, yes this is to be expected. You should not be using the property wrappers directly in the body of a view and it should only be used as an instance property of the view. Is there a reason you were using it in the body of a view? |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
SublimeAbode
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi @SublimeAbode, yes this is to be expected. You should not be using the property wrappers directly in the body of a view and it should only be used as an instance property of the view.
Is there a reason you were using it in the body of a view?