Skip to content

Commit 9b26c39

Browse files
refactor: improve main thread dispatch for Flutter method channel calls
- Add DispatchQueue.main.async for each method call to ensure platform channel messages are sent on the correct thread
1 parent 6993bfc commit 9b26c39

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

ios/Classes/HelperClasses/Utils.swift

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,10 @@ public class Utils: NSObject {
6565
"url" : url,
6666
"params" : logEvent as Any
6767
]
68-
SwiftOptimizelyFlutterSdkPlugin.channel.invokeMethod("\(NotificationType.logEvent)CallbackListener", arguments: [RequestParameterKey.sdkKey: sdkKey, RequestParameterKey.notificationId: id, RequestParameterKey.notificationType: NotificationType.logEvent, RequestParameterKey.notificationPayload: listenerDict])
68+
// Dispatch to main thread for Flutter method channel call. Platform channel messages must be sent on the platform thread to avoid data loss or crashes.
69+
DispatchQueue.main.async {
70+
SwiftOptimizelyFlutterSdkPlugin.channel.invokeMethod("\(NotificationType.logEvent)CallbackListener", arguments: [RequestParameterKey.sdkKey: sdkKey, RequestParameterKey.notificationId: id, RequestParameterKey.notificationType: NotificationType.logEvent, RequestParameterKey.notificationPayload: listenerDict])
71+
}
6972
}
7073

7174
return listener
@@ -94,7 +97,10 @@ public class Utils: NSObject {
9497
"attributes" : attributes as Any,
9598
"variation" : variation
9699
]
97-
SwiftOptimizelyFlutterSdkPlugin.channel.invokeMethod("\(NotificationType.activate)CallbackListener", arguments: [RequestParameterKey.sdkKey: sdkKey, RequestParameterKey.notificationId: id, RequestParameterKey.notificationType: NotificationType.activate, RequestParameterKey.notificationPayload: listenerDict])
100+
// Dispatch to main thread for Flutter method channel call
101+
DispatchQueue.main.async {
102+
SwiftOptimizelyFlutterSdkPlugin.channel.invokeMethod("\(NotificationType.activate)CallbackListener", arguments: [RequestParameterKey.sdkKey: sdkKey, RequestParameterKey.notificationId: id, RequestParameterKey.notificationType: NotificationType.activate, RequestParameterKey.notificationPayload: listenerDict])
103+
}
98104
}
99105
return listener
100106
}
@@ -108,7 +114,10 @@ public class Utils: NSObject {
108114
"attributes" : attributes as Any,
109115
"decisionInfo": decisionInfo
110116
]
111-
SwiftOptimizelyFlutterSdkPlugin.channel.invokeMethod("\(NotificationType.decision)CallbackListener", arguments: [RequestParameterKey.sdkKey: sdkKey, RequestParameterKey.notificationId: id, RequestParameterKey.notificationType: NotificationType.decision, RequestParameterKey.notificationPayload: listenerDict])
117+
// Dispatch to main thread for Flutter method channel call
118+
DispatchQueue.main.async {
119+
SwiftOptimizelyFlutterSdkPlugin.channel.invokeMethod("\(NotificationType.decision)CallbackListener", arguments: [RequestParameterKey.sdkKey: sdkKey, RequestParameterKey.notificationId: id, RequestParameterKey.notificationType: NotificationType.decision, RequestParameterKey.notificationPayload: listenerDict])
120+
}
112121
}
113122
return listener
114123
}
@@ -123,7 +132,9 @@ public class Utils: NSObject {
123132
"userId" : userId,
124133
// "event": event as Any, This is causing codec related exceptions on flutter side, need to debug
125134
]
126-
SwiftOptimizelyFlutterSdkPlugin.channel.invokeMethod("\(NotificationType.track)CallbackListener", arguments: [RequestParameterKey.sdkKey: sdkKey, RequestParameterKey.notificationId: id, RequestParameterKey.notificationType: NotificationType.track, RequestParameterKey.notificationPayload: listenerDict])
135+
DispatchQueue.main.async {
136+
SwiftOptimizelyFlutterSdkPlugin.channel.invokeMethod("\(NotificationType.track)CallbackListener", arguments: [RequestParameterKey.sdkKey: sdkKey, RequestParameterKey.notificationId: id, RequestParameterKey.notificationType: NotificationType.track, RequestParameterKey.notificationPayload: listenerDict])
137+
}
127138
}
128139
return listener
129140
}

0 commit comments

Comments
 (0)