Skip to content

Commit 5103952

Browse files
authored
Merge pull request #192 from optimizely/exposeDefaultEventHandler
(fix): add an objective c version of the default event dispatcher to set tim…
2 parents 4b76e64 + add4d70 commit 5103952

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

DemoObjCApp/AppDelegate.m

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
5555
// MARK: - Initialization Examples
5656

5757
-(void)initializeOptimizelySDKAsynchronous {
58-
self.optimizely = [[OptimizelyClient alloc] initWithSdkKey:kOptimizelySdkKey];
58+
DefaultEventDispatcher *eventDispacher = [[DefaultEventDispatcher alloc] initWithTimerInterval:1];
59+
60+
self.optimizely = [[OptimizelyClient alloc] initWithSdkKey:kOptimizelySdkKey logger:nil eventDispatcher:eventDispacher userProfileService:nil periodicDownloadInterval:@(5) defaultLogLevel:OptimizelyLogLevelDebug];
5961

6062
[self.optimizely startWithCompletion:^(NSData *data, NSError *error) {
6163
if (error == nil) {

OptimizelySDK/Optimizely/OptimizelyClient+ObjC.swift

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,10 +433,38 @@ extension OptimizelyClient {
433433
}
434434

435435
// MARK: - ObjC protocols
436-
437436
@objc(OPTEventDispatcher) public protocol _ObjcOPTEventDispatcher {
438437
func dispatchEvent(event:EventForDispatch, completionHandler:((Data?, NSError?) -> Void)?)
439438

440439
/// Attempts to flush the event queue if there are any events to process.
441440
func flushEvents()
442441
}
442+
443+
@available(swift, obsoleted: 1.0)
444+
@objc(DefaultEventDispatcher) public class ObjEventDispatcher : NSObject, _ObjcOPTEventDispatcher {
445+
446+
let innerEventDispatcher:DefaultEventDispatcher
447+
448+
@objc public init(timerInterval:TimeInterval) {
449+
innerEventDispatcher = DefaultEventDispatcher(timerInterval: timerInterval)
450+
}
451+
452+
public func dispatchEvent(event: EventForDispatch, completionHandler: ((Data?, NSError?) -> Void)?) {
453+
innerEventDispatcher.dispatchEvent(event: event) { (result) -> (Void) in
454+
guard let completionHandler = completionHandler else { return }
455+
456+
switch result {
457+
case .success(let value):
458+
completionHandler(value, nil)
459+
case .failure(let error):
460+
completionHandler(nil, error as NSError)
461+
}
462+
}
463+
}
464+
465+
public func flushEvents() {
466+
innerEventDispatcher.flushEvents()
467+
}
468+
469+
470+
}

0 commit comments

Comments
 (0)