Skip to content

Commit e59c9b8

Browse files
authored
refact: clean up SDK init methods with polling control (#296)
1 parent b1c0282 commit e59c9b8

24 files changed

+133
-168
lines changed

DemoObjCApp/AppDelegate.m

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/****************************************************************************
2-
* Copyright 2019, Optimizely, Inc. and contributors *
2+
* Copyright 2019-2020, Optimizely, Inc. and contributors *
33
* *
44
* Licensed under the Apache License, Version 2.0 (the "License"); *
55
* you may not use this file except in compliance with the License. *
@@ -55,11 +55,9 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
5555
// MARK: - Initialization Examples
5656

5757
-(void)initializeOptimizelySDKAsynchronous {
58-
DefaultEventDispatcher *eventDispacher = [[DefaultEventDispatcher alloc] initWithBatchSize:10 timerInterval:1 maxQueueSize:1000];
59-
6058
self.optimizely = [[OptimizelyClient alloc] initWithSdkKey:kOptimizelySdkKey
6159
logger:nil
62-
eventDispatcher:eventDispacher
60+
eventDispatcher:nil
6361
userProfileService:nil
6462
periodicDownloadInterval:@(5)
6563
defaultLogLevel:OptimizelyLogLevelDebug];
@@ -104,18 +102,42 @@ -(void)initializeOptimizelySDKSynchronous {
104102
-(void)initializeOptimizelySDKWithCustomization {
105103
// customization example (optional)
106104

107-
CustomLogger *customLogger = [[CustomLogger alloc] init];
108-
// 30 sec interval may be too frequent. This is for demo purpose.
109-
// This should be should be much larger (default = 10 mins).
110-
NSNumber *customDownloadIntervalInSecs = @(30);
105+
// You can enable background datafile polling by setting periodicDownloadInterval (polling is disabled by default)
106+
// 60 sec interval may be too frequent. This is for demo purpose. (You can set this to nil to use the recommended value of 600 secs).
107+
NSNumber *downloadIntervalInSecs = @(60);
108+
109+
// You can turn off event batching with 0 timerInterval (this means that events are sent out immediately to the server instead of saving in the local queue for batching)
110+
DefaultEventDispatcher *eventDispatcher = [[DefaultEventDispatcher alloc] initWithBatchSize:10
111+
timerInterval:0
112+
maxQueueSize:1000];
111113

114+
// customize logger
115+
CustomLogger *customLogger = [[CustomLogger alloc] init];
116+
112117
self.optimizely = [[OptimizelyClient alloc] initWithSdkKey:kOptimizelySdkKey
113118
logger:customLogger
114-
eventDispatcher:nil
119+
eventDispatcher:eventDispatcher
115120
userProfileService:nil
116-
periodicDownloadInterval:customDownloadIntervalInSecs
121+
periodicDownloadInterval:downloadIntervalInSecs
117122
defaultLogLevel:OptimizelyLogLevelDebug];
118123

124+
[self addNotificationListeners];
125+
126+
[self.optimizely startWithCompletion:^(NSData *data, NSError *error) {
127+
if (error == nil) {
128+
NSLog(@"Optimizely SDK initialized successfully!");
129+
} else {
130+
NSLog(@"Optimizely SDK initiliazation failed: %@", error.localizedDescription);
131+
}
132+
133+
[self startWithRootViewController];
134+
135+
// For sample codes for APIs, see "Samples/SamplesForAPI.swift"
136+
//[SamplesForAPI checkOptimizelyConfig:self.optimizely];
137+
}];
138+
}
139+
140+
-(void)addNotificationListeners {
119141
NSNumber *notifId;
120142
notifId = [self.optimizely.notificationCenter addDecisionNotificationListenerWithDecisionListener:^(NSString *type,
121143
NSString *userId,
@@ -135,21 +157,9 @@ -(void)initializeOptimizelySDKWithCustomization {
135157
NSDictionary<NSString *,id> *event) {
136158
NSLog(@"Received logEvent notification: %@ %@", url, event);
137159
}];
138-
139-
[self.optimizely startWithCompletion:^(NSData *data, NSError *error) {
140-
if (error == nil) {
141-
NSLog(@"Optimizely SDK initialized successfully!");
142-
} else {
143-
NSLog(@"Optimizely SDK initiliazation failed: %@", error.localizedDescription);
144-
}
145-
146-
[self startWithRootViewController];
147-
148-
// For sample codes for APIs, see "Samples/SamplesForAPI.swift"
149-
//[SamplesForAPI checkOptimizelyConfig:self.optimizely];
150-
}];
151160
}
152161

162+
153163
// MARK: - ViewControl
154164

155165
-(void)startWithRootViewController {

DemoObjCApp/DemoObjcApp.xcodeproj/xcshareddata/xcschemes/DemoObjciOS.xcscheme

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<Scheme
3-
LastUpgradeVersion = "1010"
3+
LastUpgradeVersion = "1130"
44
version = "1.3">
55
<BuildAction
66
parallelizeBuildables = "YES"
@@ -27,8 +27,6 @@
2727
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
2828
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
2929
shouldUseLaunchSchemeArgsEnv = "YES">
30-
<Testables>
31-
</Testables>
3230
<MacroExpansion>
3331
<BuildableReference
3432
BuildableIdentifier = "primary"
@@ -38,8 +36,8 @@
3836
ReferencedContainer = "container:DemoObjcApp.xcodeproj">
3937
</BuildableReference>
4038
</MacroExpansion>
41-
<AdditionalOptions>
42-
</AdditionalOptions>
39+
<Testables>
40+
</Testables>
4341
</TestAction>
4442
<LaunchAction
4543
buildConfiguration = "Debug"
@@ -61,8 +59,6 @@
6159
ReferencedContainer = "container:DemoObjcApp.xcodeproj">
6260
</BuildableReference>
6361
</BuildableProductRunnable>
64-
<AdditionalOptions>
65-
</AdditionalOptions>
6662
</LaunchAction>
6763
<ProfileAction
6864
buildConfiguration = "Release"

DemoObjCApp/DemoObjcApp.xcodeproj/xcshareddata/xcschemes/DemoObjctvOS.xcscheme

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<Scheme
3-
LastUpgradeVersion = "1010"
3+
LastUpgradeVersion = "1130"
44
version = "1.3">
55
<BuildAction
66
parallelizeBuildables = "YES"
@@ -27,8 +27,6 @@
2727
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
2828
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
2929
shouldUseLaunchSchemeArgsEnv = "YES">
30-
<Testables>
31-
</Testables>
3230
<MacroExpansion>
3331
<BuildableReference
3432
BuildableIdentifier = "primary"
@@ -38,8 +36,8 @@
3836
ReferencedContainer = "container:DemoObjcApp.xcodeproj">
3937
</BuildableReference>
4038
</MacroExpansion>
41-
<AdditionalOptions>
42-
</AdditionalOptions>
39+
<Testables>
40+
</Testables>
4341
</TestAction>
4442
<LaunchAction
4543
buildConfiguration = "Debug"
@@ -61,8 +59,6 @@
6159
ReferencedContainer = "container:DemoObjcApp.xcodeproj">
6260
</BuildableReference>
6361
</BuildableProductRunnable>
64-
<AdditionalOptions>
65-
</AdditionalOptions>
6662
</LaunchAction>
6763
<ProfileAction
6864
buildConfiguration = "Release"

DemoSwiftApp/AppDelegate.swift

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/****************************************************************************
2-
* Copyright 2019, Optimizely, Inc. and contributors *
2+
* Copyright 2019-2020, Optimizely, Inc. and contributors *
33
* *
44
* Licensed under the Apache License, Version 2.0 (the "License"); *
55
* you may not use this file except in compliance with the License. *
@@ -57,7 +57,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
5757
func initializeOptimizelySDKAsynchronous() {
5858
optimizely = OptimizelyClient(sdkKey: sdkKey, defaultLogLevel: logLevel)
5959

60-
addListeners()
60+
addNotificationListeners()
6161

6262
optimizely.start { result in
6363
switch result {
@@ -78,7 +78,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
7878

7979
optimizely = OptimizelyClient(sdkKey: sdkKey, defaultLogLevel: logLevel)
8080

81-
addListeners()
81+
addNotificationListeners()
8282

8383
do {
8484
let datafileJSON = try String(contentsOfFile: localDatafilePath, encoding: .utf8)
@@ -95,17 +95,23 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
9595
func initializeOptimizelySDKWithCustomization() {
9696
// customization example (optional)
9797

98-
let customLogger = CustomLogger()
99-
// 60 sec interval may be too frequent. This is for demo purpose.
100-
// This should be should be much larger (default = 10 mins).
101-
let customDownloadIntervalInSecs = 60
98+
// You can enable background datafile polling by setting periodicDownloadInterval (polling is disabled by default)
99+
// 60 sec interval may be too frequent. This is for demo purpose. (You can set this to nil to use the recommended value of 600 secs).
100+
let downloadIntervalInSecs: Int? = 60
101+
102+
// You can turn off event batching with 0 timerInterval (this means that events are sent out immediately to the server instead of saving in the local queue for batching)
103+
let eventDispatcher = DefaultEventDispatcher(timerInterval: 0)
102104

105+
// customize logger
106+
let customLogger = CustomLogger()
107+
103108
optimizely = OptimizelyClient(sdkKey: sdkKey,
104109
logger: customLogger,
105-
periodicDownloadInterval: customDownloadIntervalInSecs,
110+
eventDispatcher: eventDispatcher,
111+
periodicDownloadInterval: downloadIntervalInSecs,
106112
defaultLogLevel: logLevel)
107113

108-
addListeners()
114+
addNotificationListeners()
109115

110116
// initialize SDK
111117
optimizely!.start { result in
@@ -122,7 +128,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
122128
}
123129
}
124130

125-
func addListeners() {
131+
func addNotificationListeners() {
126132
// notification listeners
127133
let notificationCenter = optimizely.notificationCenter!
128134

DemoSwiftApp/DemoSwiftApp.xcodeproj/project.pbxproj

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@
393393
isa = PBXProject;
394394
attributes = {
395395
LastSwiftUpdateCheck = 1010;
396-
LastUpgradeCheck = 0940;
396+
LastUpgradeCheck = 1130;
397397
ORGANIZATIONNAME = Optimizely;
398398
TargetAttributes = {
399399
252D7DEC21C8800800134A7A = {
@@ -418,10 +418,9 @@
418418
};
419419
buildConfigurationList = EA5246B81DC7186800AF6685 /* Build configuration list for PBXProject "DemoSwiftApp" */;
420420
compatibilityVersion = "Xcode 3.2";
421-
developmentRegion = English;
421+
developmentRegion = en;
422422
hasScannedForEncodings = 0;
423423
knownRegions = (
424-
English,
425424
en,
426425
Base,
427426
);
@@ -807,6 +806,7 @@
807806
ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES;
808807
ALWAYS_SEARCH_USER_PATHS = NO;
809808
CLANG_ALLOW_NON_MODULAR_INCLUDES_IN_FRAMEWORK_MODULES = YES;
809+
CLANG_ANALYZER_LOCALIZABILITY_NONLOCALIZED = YES;
810810
CLANG_ANALYZER_NONNULL = YES;
811811
CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x";
812812
CLANG_CXX_LIBRARY = "libc++";
@@ -874,6 +874,7 @@
874874
ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES;
875875
ALWAYS_SEARCH_USER_PATHS = NO;
876876
CLANG_ALLOW_NON_MODULAR_INCLUDES_IN_FRAMEWORK_MODULES = YES;
877+
CLANG_ANALYZER_LOCALIZABILITY_NONLOCALIZED = YES;
877878
CLANG_ANALYZER_NONNULL = YES;
878879
CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x";
879880
CLANG_CXX_LIBRARY = "libc++";

DemoSwiftApp/DemoSwiftApp.xcodeproj/xcshareddata/xcschemes/DemoSwiftiOS.xcscheme

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<Scheme
3-
LastUpgradeVersion = "1010"
3+
LastUpgradeVersion = "1130"
44
version = "1.3">
55
<BuildAction
66
parallelizeBuildables = "YES"
@@ -27,6 +27,15 @@
2727
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
2828
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
2929
shouldUseLaunchSchemeArgsEnv = "YES">
30+
<MacroExpansion>
31+
<BuildableReference
32+
BuildableIdentifier = "primary"
33+
BlueprintIdentifier = "252D7DEC21C8800800134A7A"
34+
BuildableName = "DemoSwiftiOS.app"
35+
BlueprintName = "DemoSwiftiOS"
36+
ReferencedContainer = "container:DemoSwiftApp.xcodeproj">
37+
</BuildableReference>
38+
</MacroExpansion>
3039
<Testables>
3140
<TestableReference
3241
skipped = "NO">
@@ -39,17 +48,6 @@
3948
</BuildableReference>
4049
</TestableReference>
4150
</Testables>
42-
<MacroExpansion>
43-
<BuildableReference
44-
BuildableIdentifier = "primary"
45-
BlueprintIdentifier = "252D7DEC21C8800800134A7A"
46-
BuildableName = "DemoSwiftiOS.app"
47-
BlueprintName = "DemoSwiftiOS"
48-
ReferencedContainer = "container:DemoSwiftApp.xcodeproj">
49-
</BuildableReference>
50-
</MacroExpansion>
51-
<AdditionalOptions>
52-
</AdditionalOptions>
5351
</TestAction>
5452
<LaunchAction
5553
buildConfiguration = "Debug"
@@ -71,8 +69,6 @@
7169
ReferencedContainer = "container:DemoSwiftApp.xcodeproj">
7270
</BuildableReference>
7371
</BuildableProductRunnable>
74-
<AdditionalOptions>
75-
</AdditionalOptions>
7672
</LaunchAction>
7773
<ProfileAction
7874
buildConfiguration = "Release"

DemoSwiftApp/DemoSwiftApp.xcodeproj/xcshareddata/xcschemes/DemoSwifttvOS.xcscheme

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<Scheme
3-
LastUpgradeVersion = "1010"
3+
LastUpgradeVersion = "1130"
44
version = "1.3">
55
<BuildAction
66
parallelizeBuildables = "YES"
@@ -27,6 +27,15 @@
2727
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
2828
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
2929
shouldUseLaunchSchemeArgsEnv = "YES">
30+
<MacroExpansion>
31+
<BuildableReference
32+
BuildableIdentifier = "primary"
33+
BlueprintIdentifier = "252D7E1021C8804E00134A7A"
34+
BuildableName = "DemoSwifttvOS.app"
35+
BlueprintName = "DemoSwifttvOS"
36+
ReferencedContainer = "container:DemoSwiftApp.xcodeproj">
37+
</BuildableReference>
38+
</MacroExpansion>
3039
<Testables>
3140
<TestableReference
3241
skipped = "NO">
@@ -39,17 +48,6 @@
3948
</BuildableReference>
4049
</TestableReference>
4150
</Testables>
42-
<MacroExpansion>
43-
<BuildableReference
44-
BuildableIdentifier = "primary"
45-
BlueprintIdentifier = "252D7E1021C8804E00134A7A"
46-
BuildableName = "DemoSwifttvOS.app"
47-
BlueprintName = "DemoSwifttvOS"
48-
ReferencedContainer = "container:DemoSwiftApp.xcodeproj">
49-
</BuildableReference>
50-
</MacroExpansion>
51-
<AdditionalOptions>
52-
</AdditionalOptions>
5351
</TestAction>
5452
<LaunchAction
5553
buildConfiguration = "Debug"
@@ -71,8 +69,6 @@
7169
ReferencedContainer = "container:DemoSwiftApp.xcodeproj">
7270
</BuildableReference>
7371
</BuildableProductRunnable>
74-
<AdditionalOptions>
75-
</AdditionalOptions>
7672
</LaunchAction>
7773
<ProfileAction
7874
buildConfiguration = "Release"

DemoSwiftApp/Samples/SamplesForAPI.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//
22
/****************************************************************************
3-
* Copyright 2019, Optimizely, Inc. and contributors *
3+
* Copyright 2019-2020, Optimizely, Inc. and contributors *
44
* *
55
* Licensed under the Apache License, Version 2.0 (the "License"); *
66
* you may not use this file except in compliance with the License. *
@@ -164,7 +164,7 @@ class SamplesForAPI {
164164

165165
variationKeys.forEach { varKey in
166166
let variation = variationsMap[varKey]!
167-
print("[OptimizelyConfig] -- variation = { key: \(varKey), id: \(variation.id), featureEnabled: \(variation.featureEnabled)")
167+
print("[OptimizelyConfig] -- variation = { key: \(varKey), id: \(variation.id), featureEnabled: \(String(describing: variation.featureEnabled))")
168168

169169
let variablesMap = variationsMap[varKey]!.variablesMap
170170
let variableKeys = variablesMap.keys

0 commit comments

Comments
 (0)