Skip to content

Runtime Warning: "Publishing changes from within view updates is not allowed" when using .onReceive with ObservableObject in iOS 16 #22

@avasveliya

Description

@avasveliya

We are encountering the following issue in iOS 16 when using the .onReceive modifier with an ObservableObject:

.onReceive(form.manager.$allValid) { isValid in
      self.isSaveDisabled = !isValid
}

The error message displayed is:
Publishing changes from within view updates is not allowed, this will cause undefined behavior.

As per our understanding, in iOS 16, it is required for the ObservableObject class to be annotated with @mainactor in order to avoid such issues.

Steps to Reproduce:

  • Use the code snippet above where form.manager.$allValid is being observed.
  • Observe the behavior when this code executes.

Expected Behavior: The code should function without throwing the error, and changes should be made to isSaveDisabled without any warnings or undefined behavior.

Actual Behavior: We are seeing the error message: "Publishing changes from within view updates is not allowed, this will cause undefined behavior."

Please refer to the attached screenshot for the position where this error is being triggered.

Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions