From bc8704b03891372566f3450ed02a082ecb0ae4a9 Mon Sep 17 00:00:00 2001 From: Levi Bostian Date: Thu, 1 Aug 2024 12:32:42 -0500 Subject: [PATCH] fix: send native to flutter messages on main thread (#134) --- ios/Classes/SwiftCustomerIoPlugin.swift | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/ios/Classes/SwiftCustomerIoPlugin.swift b/ios/Classes/SwiftCustomerIoPlugin.swift index bcd0ff6..107a56d 100644 --- a/ios/Classes/SwiftCustomerIoPlugin.swift +++ b/ios/Classes/SwiftCustomerIoPlugin.swift @@ -185,14 +185,18 @@ public class SwiftCustomerIoPlugin: NSObject, FlutterPlugin { DispatchQueue.main.async { MessagingInApp.shared.initialize(eventListener: CustomerIOInAppEventListener( invokeMethod: {method,args in - self.invokeMethodInBackground(method, args) + self.invokeMethod(method, args) }) ) } } - func invokeMethodInBackground(_ method: String, _ args: Any?) { - DispatchQueue.global(qos: .background).async { + func invokeMethod(_ method: String, _ args: Any?) { + // When sending messages from native code to Flutter, it's required to do it on main thread. + // Learn more: + // * https://docs.flutter.dev/platform-integration/platform-channels#channels-and-platform-threading + // * https://linear.app/customerio/issue/MBL-358/ + DispatchQueue.main.async { self.methodChannel.invokeMethod(method, arguments: args) } }