Skip to content

Commit 075323f

Browse files
committed
Fix for compiler issue
1 parent b72f62a commit 075323f

1 file changed

Lines changed: 13 additions & 7 deletions

File tree

sdks/swift/Sources/DittoChatCore/PushNotifications/ChatNotificationManager.swift

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,15 +91,20 @@ final class ChatNotificationManager {
9191
LIMIT 50
9292
"""
9393

94+
// Pass self as nonisolated(unsafe) to remove it from the @MainActor region before
95+
// it crosses into makeObserver. Swift 6.3 region isolation flags `self` as a sending
96+
// value when it flows from an @MainActor method into a nonisolated one; the
97+
// nonisolated(unsafe) local opts the reference out of that tracking entirely.
98+
nonisolated(unsafe) let unsafeSelf: ChatNotificationManager = self
99+
94100
do {
95101
// registerObserver is called from a nonisolated helper so that the closure passed
96102
// to Ditto is created in a nonisolated context. Swift 6.3 injects
97103
// _swift_task_checkIsolatedSwift into the prologue of any closure created inside an
98-
// @MainActor method — even if all captures are nonisolated(unsafe) — crashing when
99-
// Ditto delivers the callback on utility-qos. Moving closure creation into a
100-
// nonisolated method removes that coloring entirely.
104+
// @MainActor method — crashing when Ditto delivers the callback on utility-qos.
101105
let observer = try makeObserver(store: ditto.store, query: query,
102-
roomId: room.id, roomName: room.name)
106+
roomId: room.id, roomName: room.name,
107+
owner: unsafeSelf)
103108
roomObservers[room.id] = observer
104109
} catch {
105110
print("ChatNotificationManager: failed to register observer for room \(room.id): \(error)")
@@ -113,14 +118,15 @@ final class ChatNotificationManager {
113118
store: DittoStore,
114119
query: String,
115120
roomId: String,
116-
roomName: String
121+
roomName: String,
122+
owner: ChatNotificationManager
117123
) throws -> DittoStoreObserver {
118-
nonisolated(unsafe) weak var weakSelf: ChatNotificationManager? = self
124+
nonisolated(unsafe) weak var weakOwner: ChatNotificationManager? = owner
119125
return try store.registerObserver(query: query, arguments: ["roomId": roomId]) { result in
120126
let messages = result.items.compactMap { Message(value: $0.value) }
121127
DispatchQueue.main.async {
122128
MainActor.assumeIsolated {
123-
weakSelf?.handle(messages: messages, roomId: roomId, roomName: roomName)
129+
weakOwner?.handle(messages: messages, roomId: roomId, roomName: roomName)
124130
}
125131
}
126132
}

0 commit comments

Comments
 (0)