Skip to content

Commit 7ae86de

Browse files
chore: update log messages and method channel handling
- Update log message format in CustomLogger class - Refactor main thread dispatch logic for method channel calls in Utils.swift - Enhance error handling and main thread dispatch in OptimizelyFlutterLogger.swift - Modify method channel creation and task queue handling in SwiftOptimizelyFlutterSdkPlugin.swift
1 parent 49ed2dc commit 7ae86de

File tree

4 files changed

+12
-28
lines changed

4 files changed

+12
-28
lines changed

example/lib/custom_logger.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ class CustomLogger implements OptimizelyLogger {
55
@override
66
void log(OptimizelyLogLevel level, String message) {
77
if (kDebugMode) {
8-
print('[Flutter LOGGER] ${level.name}: $message');
8+
print('[OPTIMIZELY] ${level.name.toUpperCase()}: $message');
99
}
1010
}
1111
}

ios/Classes/HelperClasses/Utils.swift

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,7 @@ public class Utils: NSObject {
6565
"url" : url,
6666
"params" : logEvent as Any
6767
]
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-
}
68+
SwiftOptimizelyFlutterSdkPlugin.channel.invokeMethod("\(NotificationType.logEvent)CallbackListener", arguments: [RequestParameterKey.sdkKey: sdkKey, RequestParameterKey.notificationId: id, RequestParameterKey.notificationType: NotificationType.logEvent, RequestParameterKey.notificationPayload: listenerDict])
7269
}
7370

7471
return listener
@@ -97,10 +94,7 @@ public class Utils: NSObject {
9794
"attributes" : attributes as Any,
9895
"variation" : variation
9996
]
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-
}
97+
SwiftOptimizelyFlutterSdkPlugin.channel.invokeMethod("\(NotificationType.activate)CallbackListener", arguments: [RequestParameterKey.sdkKey: sdkKey, RequestParameterKey.notificationId: id, RequestParameterKey.notificationType: NotificationType.activate, RequestParameterKey.notificationPayload: listenerDict])
10498
}
10599
return listener
106100
}
@@ -114,10 +108,7 @@ public class Utils: NSObject {
114108
"attributes" : attributes as Any,
115109
"decisionInfo": decisionInfo
116110
]
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-
}
111+
SwiftOptimizelyFlutterSdkPlugin.channel.invokeMethod("\(NotificationType.decision)CallbackListener", arguments: [RequestParameterKey.sdkKey: sdkKey, RequestParameterKey.notificationId: id, RequestParameterKey.notificationType: NotificationType.decision, RequestParameterKey.notificationPayload: listenerDict])
121112
}
122113
return listener
123114
}
@@ -132,9 +123,7 @@ public class Utils: NSObject {
132123
"userId" : userId,
133124
// "event": event as Any, This is causing codec related exceptions on flutter side, need to debug
134125
]
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-
}
126+
SwiftOptimizelyFlutterSdkPlugin.channel.invokeMethod("\(NotificationType.track)CallbackListener", arguments: [RequestParameterKey.sdkKey: sdkKey, RequestParameterKey.notificationId: id, RequestParameterKey.notificationType: NotificationType.track, RequestParameterKey.notificationPayload: listenerDict])
138127
}
139128
return listener
140129
}

ios/Classes/OptimizelyFlutterLogger.swift

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,12 @@ public class OptimizelyFlutterLogger: NSObject, OPTLogger {
2828
return
2929
}
3030

31-
// Ensure logging happens on main thread as FlutterMethodChannel requires it
32-
if Thread.isMainThread {
33-
// Already on main thread
31+
// https://docs.flutter.dev/platform-integration/platform-channels#jumping-to-the-main-thread-in-ios
32+
DispatchQueue.main.async {
3433
channel.invokeMethod("log", arguments: [
3534
"level": level.rawValue,
3635
"message": message
3736
])
38-
} else {
39-
// Switch to main thread
40-
DispatchQueue.main.sync {
41-
channel.invokeMethod("log", arguments: [
42-
"level": level.rawValue,
43-
"message": message
44-
])
45-
}
4637
}
4738
}
4839
}

ios/Classes/SwiftOptimizelyFlutterSdkPlugin.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,11 @@ public class SwiftOptimizelyFlutterSdkPlugin: NSObject, FlutterPlugin {
4545
registrar.addMethodCallDelegate(instance, channel: channel)
4646

4747
// Separate logger channel for outgoing log calls
48-
let loggerChannel = FlutterMethodChannel(name: OptimizelyFlutterLogger.LOGGER_CHANNEL, binaryMessenger: registrar.messenger())
48+
let taskQueue = registrar.messenger().makeBackgroundTaskQueue?()
49+
let loggerChannel = FlutterMethodChannel(name: OptimizelyFlutterLogger.LOGGER_CHANNEL,
50+
binaryMessenger: registrar.messenger(),
51+
codec: FlutterStandardMethodCodec.sharedInstance(),
52+
taskQueue: taskQueue)
4953
OptimizelyFlutterLogger.setChannel(loggerChannel)
5054
}
5155

0 commit comments

Comments
 (0)