Skip to content

Commit b968bce

Browse files
wangjoshuahalda-optimizely
authored andcommitted
set cached client after successful initialization
1 parent 554cc84 commit b968bce

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

OptimizelySDKShared/OptimizelySDKShared/OPTLYManager.m

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,13 @@
2222
#import <OptimizelySDKCore/OPTLYLogger.h>
2323
#import <OptimizelySDKCore/OPTLYLoggerMessages.h>
2424

25-
@implementation OPTLYManager
25+
@interface OPTLYManager ()
26+
27+
@property (strong, readwrite, nonatomic, nullable) OPTLYClient *optimizelyClient;
28+
29+
@end
2630

27-
OPTLYClient *optimizelyClient;
31+
@implementation OPTLYManager
2832

2933
+ (instancetype)initWithBuilderBlock:(OPTLYManagerBuilderBlock)block {
3034
return [[self alloc] initWithBuilder:[OPTLYManagerBuilder builderWithBlock:block]];
@@ -68,16 +72,21 @@ - (instancetype)initWithBuilder:(OPTLYManagerBuilder *)builder {
6872
}
6973

7074
- (OPTLYClient *)initializeClient {
71-
return [OPTLYClient initWithBuilderBlock:^(OPTLYClientBuilder * _Nonnull builder) {
75+
OPTLYClient *client = [OPTLYClient initWithBuilderBlock:^(OPTLYClientBuilder * _Nonnull builder) {
7276
builder.datafile = self.datafile;
7377
}];
78+
if (client.optimizely != nil) {
79+
self.optimizelyClient = client;
80+
}
81+
return client;
7482
}
7583

7684
- (OPTLYClient *)initializeClientWithDatafile:(NSData *)datafile {
7785
OPTLYClient *client = [OPTLYClient initWithBuilderBlock:^(OPTLYClientBuilder * _Nonnull builder) {
7886
builder.datafile = datafile;
7987
}];
8088
if (client.optimizely != nil) {
89+
self.optimizelyClient = client;
8190
return client;
8291
}
8392
else {
@@ -96,13 +105,16 @@ - (void)initializeClientWithCallback:(void (^)(NSError * _Nullable, OPTLYClient
96105
OPTLYClient *client = [OPTLYClient initWithBuilderBlock:^(OPTLYClientBuilder * _Nonnull builder) {
97106
builder.datafile = data;
98107
}];
108+
if (client.optimizely != nil) {
109+
self.optimizelyClient = client;
110+
}
99111
callback(nil, client);
100112
}
101113
}];
102114
}
103115

104116
- (OPTLYClient *)getOptimizely {
105-
return optimizelyClient;
117+
return self.optimizelyClient;
106118
}
107119

108120
@end

OptimizelySDKShared/OptimizelySDKSharedTests/OPTLYManagerTest.m

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ - (void)testInitializeClientWithDefaults {
8080
XCTAssertNotNil(client);
8181
XCTAssertNotNil(client.optimizely);
8282
XCTAssertNotNil(client.logger);
83-
83+
XCTAssertEqual(client, manager.getOptimizely);
84+
8485
[self checkConfigIsUsingDefaultDatafile:client.optimizely.config];
8586
}
8687

@@ -103,6 +104,7 @@ - (void)testInitializeClientWithCustomDatafile {
103104
XCTAssertNotNil(client);
104105
XCTAssertNotNil(client.optimizely);
105106
XCTAssertNotNil(client.logger);
107+
XCTAssertEqual(client, manager.getOptimizely);
106108

107109
[self checkConfigIsUsingAlternativeDatafile:client.optimizely.config];
108110
}
@@ -132,8 +134,11 @@ - (void)testInitializeClientAsync {
132134
// setup async expectation
133135
__weak XCTestExpectation *expectation = [self expectationWithDescription:@"testInitializeClientAsync"];
134136
// initialize client
137+
__block OPTLYClient *optimizelyClient;
135138
[manager initializeClientWithCallback:^(NSError * _Nullable error, OPTLYClient * _Nullable client) {
136139

140+
// retain a reference to the client
141+
optimizelyClient = client;
137142
// check client in callback
138143
XCTAssertNotNil(client);
139144
XCTAssertNotNil(client.optimizely, @"Client needs to have an optimizely instance");
@@ -144,6 +149,7 @@ - (void)testInitializeClientAsync {
144149

145150
// wait for async start to finish
146151
[self waitForExpectationsWithTimeout:2 handler:nil];
152+
XCTAssertEqual(optimizelyClient, manager.getOptimizely);
147153
}
148154

149155
- (void)checkConfigIsUsingDefaultDatafile: (OPTLYProjectConfig *)config {

0 commit comments

Comments
 (0)