# Runtime warning: "Modifying state during view update, this will cause undefined behavior."

**URL:** <https://forums.swift.org/t/runtime-warning-modifying-state-during-view-update-this-will-cause-undefined-behavior/77364>\
**Category:** Using Swift\
**Created:** [January 21, 2025, 3:10pm UTC](https://forums.swift.org/t/runtime-warning-modifying-state-during-view-update-this-will-cause-undefined-behavior/77364 "2025-01-21T15:10:40Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![carlhung](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/carlhung/32/4446_2.png) [@carlhung](https://forums.swift.org/u/carlhung)\
**Post date:** [January 21, 2025, 3:10pm UTC](https://forums.swift.org/t/runtime-warning-modifying-state-during-view-update-this-will-cause-undefined-behavior/77364/1 "2025-01-21T15:10:40Z")

</div>

I wrapped the UILabel into swiftui.

```swift
struct AttributedLabel: UIViewRepresentable {
        
        // ...
       
        func updateUIView(_ uiView: UILabel, context: Context) {
            uiView.frame.size.width = availableWidth
            uiView.attributedText = nsAttributedText
            uiView.sizeToFit()
            // Modifying state during view update, this will cause undefined behavior.
            self.dynamicHeight = uiView.frame.size.height
        }
    }

```

the line of `self.dynamicHeight = uiView.frame.size.height` I may get a running warning:  
`Modifying state during view update, this will cause undefined behavior.`

I know I can wrap it into:

```swift
// Or using, Task { @MainActor in }
DispatchQueue.main.async {
    self.dynamicHeight = uiView.frame.size.height
}

```

But for me, it doesn't make sense, as `dynamicHeight` is type of `@Binding @MainActor var dynamicHeight: CGFloat` and `uiView` is `UILabel` which is `@MainActor`. Also, `@MainActor func updateUIView(_ uiView:, context:)` is `@MainActor` as well.  
Is it a bug?

---

<div class="post-metadata">

**Author:** ![bbrk24](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/bbrk24/32/27370_2.png) [@bbrk24](https://forums.swift.org/u/bbrk24)\
**Post date:** [January 21, 2025, 4:40pm UTC](https://forums.swift.org/t/runtime-warning-modifying-state-during-view-update-this-will-cause-undefined-behavior/77364/2 "2025-01-21T16:40:22Z")

</div>

This has nothing to do with actors -- you just aren't allowed to assign to `@Binding` (or `@State`) properties during a view update.

However, I believe SwiftUI questions are generally off-topic for this forum, so I would encourage further questions to go somewhere else like StackOverflow.

---

<div class="post-metadata">

**Author:** ![carlhung](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/carlhung/32/4446_2.png) [@carlhung](https://forums.swift.org/u/carlhung)\
**Post date:** [January 21, 2025, 4:59pm UTC](https://forums.swift.org/t/runtime-warning-modifying-state-during-view-update-this-will-cause-undefined-behavior/77364/3 "2025-01-21T16:59:39Z")

</div>

it is related to the actor. because to fix this issue, i need to either use `DispatchQueue.main.async` or `Task { @MainActor in }`.  
This means i need to transfer it to main thread to get rid of this warning.  
But according to swift strict concurreny, the code supposes to run on main thread without calling `DispatchQueue.main.async` or `Task { @MainActor in }`. this can be langugage issue.

---

<div class="post-metadata">

**Author:** ![bbrk24](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/bbrk24/32/27370_2.png) [@bbrk24](https://forums.swift.org/u/bbrk24)\
**Post date:** [January 21, 2025, 5:00pm UTC](https://forums.swift.org/t/runtime-warning-modifying-state-during-view-update-this-will-cause-undefined-behavior/77364/4 "2025-01-21T17:00:43Z")

</div>

No, `updateUIView` is already running on the main actor. Both of the things you wrote _schedule work to run on the main thread at some point in the future_, so the callback doesn't run until the view update has finished.

---

<div class="post-metadata">

**Author:** ![carlhung](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/carlhung/32/4446_2.png) [@carlhung](https://forums.swift.org/u/carlhung)\
**Post date:** [January 21, 2025, 5:04pm UTC](https://forums.swift.org/t/runtime-warning-modifying-state-during-view-update-this-will-cause-undefined-behavior/77364/5 "2025-01-21T17:04:22Z")

</div>

I know updateUIView is running on main thread. but `DispatchQueue.main.async` or `Task { @MainActor in }` are also running on the main thread. as main thread is serial. it should not require `DispatchQueue.main.async` or `Task { @MainActor in }`.

---

<div class="post-metadata">

**Author:** ![lassejansen](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lassejansen/32/4712_2.png) [@lassejansen](https://forums.swift.org/u/lassejansen)\
**Post date:** [January 21, 2025, 5:20pm UTC](https://forums.swift.org/t/runtime-warning-modifying-state-during-view-update-this-will-cause-undefined-behavior/77364/6 "2025-01-21T17:20:26Z")

</div>

If you can use iOS 16 as deployment target use `sizeThatFits` for dynamic height calculation:

> **[sizeThatFits(\_:uiView:context:) | Apple Developer Documentation](https://developer.apple.com/documentation/swiftui/uiviewrepresentable/sizethatfits(_:uiview:context:)-5tdxh)**
>
> Given a proposed size, returns the preferred size of the composite view.

---

<div class="post-metadata">

**Author:** ![lassejansen](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lassejansen/32/4712_2.png) [@lassejansen](https://forums.swift.org/u/lassejansen)\
**Post date:** [January 21, 2025, 5:29pm UTC](https://forums.swift.org/t/runtime-warning-modifying-state-during-view-update-this-will-cause-undefined-behavior/77364/7 "2025-01-21T17:29:33Z")

</div>

The warning you see isn't a concurrency warning from the compiler, it's an internal warning of the SwiftUI framework that notifies you that it doesn't support modifying parent views during a view update pass.

---

<div class="post-metadata">

**Author:** ![carlhung](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/carlhung/32/4446_2.png) [@carlhung](https://forums.swift.org/u/carlhung)\
**Post date:** [January 21, 2025, 6:18pm UTC](https://forums.swift.org/t/runtime-warning-modifying-state-during-view-update-this-will-cause-undefined-behavior/77364/8 "2025-01-21T18:18:43Z")

</div>

If it isn’t concurrency, why does it need to wrap inside `DispatchQueue.main.async` or `Task { @MainActor in }` to fix the issue?

---

<div class="post-metadata">

**Author:** ![lassejansen](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lassejansen/32/4712_2.png) [@lassejansen](https://forums.swift.org/u/lassejansen)\
**Post date:** [January 21, 2025, 6:24pm UTC](https://forums.swift.org/t/runtime-warning-modifying-state-during-view-update-this-will-cause-undefined-behavior/77364/9 "2025-01-21T18:24:51Z")

</div>

Because the framework cannot detect the potential layout loop anymore. The layout is probably still undefined. Before `sizeThatFits` I also used the `DispatchQueue` hack sometimes, but there was often some visible layout shifting going on, and sometimes the last text lines were missing.

---

<div class="post-metadata">

**Author:** ![let\_zeppelin](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/let_zeppelin/32/31541_2.png) [@let\_zeppelin](https://forums.swift.org/u/let_zeppelin)\
**Post date:** [January 21, 2025, 11:28pm UTC](https://forums.swift.org/t/runtime-warning-modifying-state-during-view-update-this-will-cause-undefined-behavior/77364/10 "2025-01-21T23:28:42Z")

</div>

Is this simplified sample code? Because if it is not – as of iOS 15 SwiftUI's `Text` view can display AttributedString (which you can initialize from NSAttributedString) just fine.

---

<div class="post-metadata">

**Author:** ![carlhung](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/carlhung/32/4446_2.png) [@carlhung](https://forums.swift.org/u/carlhung)\
**Post date:** [January 22, 2025, 1:45am UTC](https://forums.swift.org/t/runtime-warning-modifying-state-during-view-update-this-will-cause-undefined-behavior/77364/11 "2025-01-22T01:45:01Z")

</div>

Yes, I know. our app also supports ios 14.
