Skip to content

Commit 5e281dd

Browse files
committed
Fix compilation error of Swift Concurrency
1 parent d5eda14 commit 5e281dd

File tree

2 files changed

+19
-11
lines changed

2 files changed

+19
-11
lines changed

Sources/REText/AsyncLayer.swift

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,11 @@ public class AsyncLayer: CALayer, @unchecked Sendable {
342342
let isCancelled: @Sendable () -> Bool = { value != sentinel.value }
343343
let backgroundColor = (opaque && self.backgroundColor != nil) ? self.backgroundColor : nil
344344

345-
Task(priority: .high) {
345+
// Capture self as nonisolated(unsafe) to avoid data race warnings
346+
// This is safe because we only access self on the MainActor
347+
nonisolated(unsafe) let unsafeSelf = self
348+
349+
Task(priority: .high) { @Sendable in
346350
await RenderActorPool.next().render {
347351
if isCancelled() {
348352
_ = backgroundColor
@@ -374,16 +378,16 @@ public class AsyncLayer: CALayer, @unchecked Sendable {
374378

375379
if isCancelled() {
376380
await MainActor.run {
377-
task.didDisplay?(self, false)
381+
task.didDisplay?(unsafeSelf, false)
378382
}
379383
return
380384
}
381385
await MainActor.run {
382386
if isCancelled() {
383-
task.didDisplay?(self, false)
387+
task.didDisplay?(unsafeSelf, false)
384388
} else {
385-
self.contents = image.cgImage
386-
task.didDisplay?(self, true)
389+
unsafeSelf.contents = image.cgImage
390+
task.didDisplay?(unsafeSelf, true)
387391
}
388392
}
389393
}

Sources/REText/Attributes/TextAttachment.swift

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,10 @@ open class TextAttachment: NSTextAttachment, @unchecked Sendable {
8484
open var content: Content? {
8585
didSet {
8686
guard content != oldValue else { return }
87-
syncOnMain { contentSize = content?.size ?? .zero }
87+
nonisolated(unsafe) let unsafeSelf = self
88+
MainActor.assumeIsolated {
89+
unsafeSelf.contentSize = unsafeSelf.content?.size ?? .zero
90+
}
8891
}
8992
}
9093

@@ -164,12 +167,13 @@ open class TextAttachment: NSTextAttachment, @unchecked Sendable {
164167
}
165168

166169
case .layer(let layer):
167-
syncOnMain {
168-
if layer.frame != rect {
169-
layer.frame = rect
170+
nonisolated(unsafe) let unsafeLayer = layer
171+
MainActor.assumeIsolated {
172+
if unsafeLayer.frame != rect {
173+
unsafeLayer.frame = rect
170174
}
171-
if let textView, layer.superlayer != textView.layer {
172-
textView.layer.addSublayer(layer)
175+
if let textView, unsafeLayer.superlayer != textView.layer {
176+
textView.layer.addSublayer(unsafeLayer)
173177
}
174178
}
175179
default:

0 commit comments

Comments
 (0)