Skip to content

Commit

Permalink
Add main queue preconditions within BlueprintView (#518)
Browse files Browse the repository at this point in the history
This adds main queue preconditions to BlueprintView to help diagnose
some `EXC_BAD_ACCESS` crashes we've seen in Bugsnag.
  • Loading branch information
robmaceachern authored Oct 30, 2024
2 parents 6b5cd5b + d2ded60 commit 8312672
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
29 changes: 29 additions & 0 deletions BlueprintUI/Sources/BlueprintView/BlueprintView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,16 @@ public final class BlueprintView: UIView {

/// Clears any sizing caches, invalidates the `intrinsicContentSize` of the
/// view, and marks the view as needing a layout.
@MainActor
private func setNeedsViewHierarchyUpdate() {
MainActor.preconditionIsolated(
"""
setNeedsViewHierarchyUpdate() must only execute on the main actor.
Examine your stack trace to determine what caused this to be called
off of the main actor.
"""
)

invalidateIntrinsicContentSize()
sizesThatFit.removeAll()
Expand All @@ -364,7 +373,17 @@ public final class BlueprintView: UIView {
setNeedsLayout()
}

@MainActor
private func updateViewHierarchyIfNeeded() {
MainActor.preconditionIsolated(
"""
updateViewHierarchyIfNeeded() must only execute on the main actor.
Examine your stack trace to determine what caused this to be called
off of the main actor.
"""
)

guard needsViewHierarchyUpdate || bounds != lastViewHierarchyUpdateBounds else { return }

precondition(
Expand Down Expand Up @@ -612,7 +631,17 @@ extension BlueprintView {
node.viewDescription.viewType == type(of: view)
}

@MainActor
fileprivate func update(node: NativeViewNode, context: UpdateContext) -> UpdateResult {
MainActor.preconditionIsolated(
"""
BlueprintView.NativeViewController.update(node:context:) must
only execute on the main actor.
Examine your stack trace to determine what caused this to be
called off of the main actor.
"""
)

assert(node.viewDescription.viewType == type(of: view))

Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- `BlueprintView` has added preconditions to some methods to ensure they are only invoked on the main queue.

### Removed

### Changed
Expand Down

0 comments on commit 8312672

Please sign in to comment.