diff --git a/ios/RowndPlugin.swift b/ios/RowndPlugin.swift index 2793901..b0e2675 100644 --- a/ios/RowndPlugin.swift +++ b/ios/RowndPlugin.swift @@ -43,14 +43,17 @@ class RowndPlugin: NSObject { // Note: Subsequent calls to configure() will not reinitialize the state subscription // if it has already been initialized. This is intentional to prevent duplicate subscriptions. if self.state == nil { - let initializedState = Rownd.getInstance().state().subscribe { $0 } - self.state = initializedState - self.stateCancellable = initializedState.$current.sink { newState in - do { - RowndPluginEventEmitter.emitter.sendEvent( - withName: "update_state", body: try newState.toDictionary()) - } catch { - print("Failed to encode Rownd state: \(String(describing: error))") + Task { @MainActor [weak self] in + guard let self = self, self.state == nil else { return } + let initializedState = Rownd.getInstance().state().subscribe { $0 } + self.state = initializedState + self.stateCancellable = initializedState.$current.sink { newState in + do { + RowndPluginEventEmitter.emitter.sendEvent( + withName: "update_state", body: try newState.toDictionary()) + } catch { + print("Failed to encode Rownd state: \(String(describing: error))") + } } } }