Hello,
I’m encountering the error "Publishing changes from within view updates is not allowed, this will cause undefined behavior". while developing an app using SwiftUI and ARKit.
I have a ObjectTracking class where I update some @Published variables inside a function called processObjectAttributes after detecting objects. However, when I try to update these state variables in the View (like isPositionChecked, etc.), the error keeps appearing.
Here is a simplified version of my code:
class ObjectTracking: ObservableObject {
@Published var isPositionChecked: Bool = false
@Published var isSizeChecked: Bool = false
@Published var isOrientationChecked: Bool = false
func checkAttributes(objectAnchor: ARObjectAnchor,
_ left: ARObjectAnchor.AttributeLocation,
_ right: ARObjectAnchor.AttributeLocation? = nil,
threshold: Float) -> Bool {
let attributes = objectAnchor.attributes
guard let leftValue = attributes[left]?.floatValue else { return false }
let rightValue = right != nil ? attributes[right!]?.floatValue ?? 0 : 0
return leftValue > threshold && (right == nil || rightValue > threshold)
}
func isComplete(objectAnchor: ARObjectAnchor) -> Bool {
isPositionChecked = checkAttributes(objectAnchor: objectAnchor, .positionLeft, .positionRight, threshold: 0.5)
isSizeChecked = checkAttributes(objectAnchor: objectAnchor, .sizeLeft, .sizeRight, threshold: 0.3)
isOrientationChecked = checkAttributes(objectAnchor: objectAnchor, .orientationLeft, .orientationRight, threshold: 0.3)
return isPositionChecked && isSizeChecked && isOrientationChecked
}
func processObjectAttributes(objectAnchor: ARObjectAnchor) {
currentObjectAnchor = objectAnchor
}
}
In my View, I am using @ObservedObject to observe the state of these variables, but the error persists when I try to update them during view rendering.
Could anyone help me understand why this error occurs and how to avoid it? I understand that state should not be updated during view rendering, but I can’t find a solution that works in this case.
Thank you in advance for your help!
Post
Replies
Boosts
Views
Activity
Hi everyone,
I've been struggling for a few weeks to integrate my Core ML Image Classifier model into my .swiftpm project, and I’m hoping someone can help.
Here’s what I’ve done so far:
I converted my .mlmodel file to .mlmodelc manually via the terminal.
In my Package.swift file, I tried both "copy" and "process" options for the resource.
The issues I’m facing:
When using "process", Xcode gives me the error:
"multiple resources named 'coremldata.bin' in target 'AppModule'."
When using "copy", the app runs, but the model doesn’t work, and the terminal shows:
"A valid manifest does not exist at path: .../Manifest.json."
I even tried creating a Manifest.json manually to test, but this led to more errors, such as:
"File format version must be in the form of major.minor.patch."
"Failed to look up root model."
To check if the problem was specific to my model, I tested other Core ML models in the same setup, but none of them worked either.
I feel stuck and unsure of how to resolve these issues. Any guidance or suggestions would be greatly appreciated. Thanks in advance! :)