Skip to content

Commit 23e6608

Browse files
author
Alexis Oyama
committed
perf(request): Add UUID logic. Delete on success and permanent failure state
1 parent 339989c commit 23e6608

File tree

8 files changed

+59
-34
lines changed

8 files changed

+59
-34
lines changed

Leanplum-SDK/Classes/Constants.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
#define HEARTBEAT_INTERVAL 15 * 60 // 15 minutes
5656

5757
#define MAX_STORED_OCCURRENCES_PER_MESSAGE 100
58-
#define MAX_STORED_API_CALLS 10000
58+
#define MAX_ACTIONS_PER_API_CALL 10000
5959

6060
#define DEFAULT_PRIORITY 1000
6161

@@ -157,6 +157,7 @@ OBJC_EXPORT NSString *LEANPLUM_DEFAULTS_PRE_LEANPLUM_INSTALL_KEY;
157157
OBJC_EXPORT NSString *LEANPLUM_DEFAULTS_SDK_VERSION;
158158
OBJC_EXPORT NSString *LEANPLUM_DEFAULTS_INBOX_KEY;
159159
OBJC_EXPORT NSString *LEANPLUM_DEFAULTS_APP_VERSION_KEY;
160+
OBJC_EXPORT NSString *LEANPLUM_DEFAULTS_UUID_KEY;
160161

161162
OBJC_EXPORT NSString *LEANPLUM_SQLITE_NAME;
162163

@@ -236,6 +237,7 @@ OBJC_EXPORT NSString *LP_PARAM_UPDATE_DATE;
236237
OBJC_EXPORT NSString *LP_PARAM_INBOX_MESSAGES;
237238
OBJC_EXPORT NSString *LP_PARAM_INBOX_MESSAGE_ID;
238239
OBJC_EXPORT NSString *LP_PARAM_RICH_PUSH_ENABLED;
240+
OBJC_EXPORT NSString *LP_PARAM_UUID;
239241

240242
OBJC_EXPORT NSString *LP_KEY_VARS;
241243
OBJC_EXPORT NSString *LP_KEY_MESSAGES;

Leanplum-SDK/Classes/Constants.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ - (id)init {
9494
NSString *LEANPLUM_DEFAULTS_SDK_VERSION = @"__leanplum_version";
9595
NSString *LEANPLUM_DEFAULTS_INBOX_KEY = @"__leanplum_newsfeed";
9696
NSString *LEANPLUM_DEFAULTS_APP_VERSION_KEY = @"leanplum_savedAppVersionKey";
97+
NSString *LEANPLUM_DEFAULTS_UUID_KEY = @"__leanplum_uuid";
9798

9899
NSString *LEANPLUM_SQLITE_NAME = @"__leanplum.sqlite";
99100

@@ -172,6 +173,7 @@ - (id)init {
172173
NSString *LP_PARAM_INBOX_MESSAGES = @"newsfeedMessages";
173174
NSString *LP_PARAM_INBOX_MESSAGE_ID = @"newsfeedMessageId";
174175
NSString *LP_PARAM_RICH_PUSH_ENABLED = @"richPushEnabled";
176+
NSString *LP_PARAM_UUID = @"uuid";
175177

176178
NSString *LP_KEY_REASON = @"reason";
177179
NSString *LP_KEY_STACK_TRACE = @"stackTrace";

Leanplum-SDK/Classes/LPEventDataManager.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,9 @@
4848
*/
4949
+ (void)deleteEventsWithLimit:(NSInteger)limit;
5050

51+
/**
52+
* Returns the number of total events stored.
53+
*/
54+
+ (NSInteger)count;
55+
5156
@end

Leanplum-SDK/Classes/LPEventDataManager.m

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,14 @@ + (void)deleteEventsWithLimit:(NSInteger)limit
9595
[[LPDatabase database] runQuery:query];
9696
}
9797

98+
+ (NSInteger)count
99+
{
100+
NSArray *rows = [[LPDatabase database] rowsFromQuery:@"SELECT count(*) FROM event;"];
101+
if (!rows || !rows.count) {
102+
return 0;
103+
}
104+
105+
return [rows.firstObject[@"count(*)"] integerValue];
106+
}
107+
98108
@end

Leanplum-SDK/Classes/LPRequestStorage.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,8 @@ - (void)migrateRequests
9797
*/
9898
- (void)saveRequests:(NSMutableArray *)requests
9999
{
100-
if (requests.count > MAX_STORED_API_CALLS) {
101-
NSRange range = NSMakeRange(0, requests.count - MAX_STORED_API_CALLS);
100+
if (requests.count > MAX_ACTIONS_PER_API_CALL) {
101+
NSRange range = NSMakeRange(0, requests.count - MAX_ACTIONS_PER_API_CALL);
102102
[requests removeObjectsInRange:range];
103103
}
104104

Leanplum-SDK/Classes/Leanplum.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3466,7 +3466,7 @@ - (BOOL)boolNamed:(NSString *)name
34663466
{
34673467
if ([Utils isNullOrEmpty:name]) {
34683468
[Leanplum throwError:@"[LPActionContext boolNamed:] Empty name parameter provided."];
3469-
return nil;
3469+
return NO;
34703470
}
34713471
LP_TRY
34723472
id object = [self objectNamed:name];

Leanplum-SDK/Classes/LeanplumRequest.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,6 @@
8181
*/
8282
- (void)sendDatasNow:(NSDictionary *)datas;
8383

84-
+ (void)pushUnsentRequests:(NSArray *)requestData;
85-
8684
- (void)downloadFile:(NSString *)path;
8785

8886
+ (int)numPendingDownloads;

Leanplum-SDK/Classes/LeanplumRequest.m

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,15 @@ + (LeanplumRequest *)post:(NSString *)apiMethod params:(NSDictionary *)params
178178
return [[LeanplumRequest alloc] initWithHttpMethod:@"POST" apiMethod:apiMethod params:params];
179179
}
180180

181+
+ (NSString *)generateUUID
182+
{
183+
NSString *uuid = [[NSUUID UUID] UUIDString];
184+
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
185+
[userDefaults setObject:uuid forKey:LEANPLUM_DEFAULTS_UUID_KEY];
186+
[userDefaults synchronize];
187+
return uuid;
188+
}
189+
181190
- (void)onResponse:(LPNetworkResponseBlock)response
182191
{
183192
_response = [response copy];
@@ -191,15 +200,16 @@ - (void)onError:(LPNetworkErrorBlock)error
191200
- (NSMutableDictionary *)createArgsDictionary
192201
{
193202
LPConstantsState *constants = [LPConstantsState sharedState];
194-
NSMutableDictionary *args = [NSMutableDictionary
195-
dictionaryWithObjectsAndKeys:
196-
_apiMethod, LP_PARAM_ACTION,
197-
deviceId ? deviceId : @"", LP_PARAM_DEVICE_ID,
198-
userId ? userId : @"", LP_PARAM_USER_ID,
199-
constants.sdkVersion, LP_PARAM_SDK_VERSION,
200-
constants.client, LP_PARAM_CLIENT,
201-
@(constants.isDevelopmentModeEnabled), LP_PARAM_DEV_MODE,
202-
[NSString stringWithFormat:@"%f", [[NSDate date] timeIntervalSince1970]], LP_PARAM_TIME, nil];
203+
NSString *timestamp = [NSString stringWithFormat:@"%f", [[NSDate date] timeIntervalSince1970]];
204+
NSMutableDictionary *args = [@{
205+
LP_PARAM_ACTION: _apiMethod,
206+
LP_PARAM_DEVICE_ID: deviceId ?: @"",
207+
LP_PARAM_USER_ID: userId ?: @"",
208+
LP_PARAM_SDK_VERSION: constants.sdkVersion,
209+
LP_PARAM_CLIENT: constants.client,
210+
LP_PARAM_DEV_MODE: @(constants.isDevelopmentModeEnabled),
211+
LP_PARAM_TIME: timestamp,
212+
} mutableCopy];
203213
if (token) {
204214
args[LP_PARAM_TOKEN] = token;
205215
}
@@ -293,10 +303,10 @@ - (void)sendNow:(BOOL)async
293303
[self sendEventually];
294304

295305
void (^operationBlock)() = ^void() {
306+
[LeanplumRequest generateUUID];
307+
296308
// Simulate pop all requests.
297-
NSArray *requestsToSend = [LPEventDataManager eventsWithLimit:MAX_STORED_API_CALLS];
298-
// !!!: Remove in next commit (New Network flow).
299-
[LPEventDataManager deleteEventsWithLimit:requestsToSend.count];
309+
NSArray *requestsToSend = [LPEventDataManager eventsWithLimit:MAX_ACTIONS_PER_API_CALL];
300310
lastSentTime = [NSDate timeIntervalSinceReferenceDate];
301311

302312
if (requestsToSend.count == 0) {
@@ -329,7 +339,6 @@ - (void)sendNow:(BOOL)async
329339
LP_TRY
330340
NSLog(@"Leanplum: Request %@ timed out", _apiMethod);
331341
[op cancel];
332-
[LeanplumRequest pushUnsentRequests:requestsToSend];
333342
if (_error != nil) {
334343
_error([NSError errorWithDomain:@"Leanplum" code:1
335344
userInfo:@{NSLocalizedDescriptionKey: @"Request timed out"}]);
@@ -365,6 +374,9 @@ - (void)sendNow:(BOOL)async
365374
}
366375
}
367376
}
377+
378+
// Delete events on success.
379+
[LPEventDataManager deleteEventsWithLimit:requestsToSend.count];
368380
LP_END_TRY
369381
if (_response != nil) {
370382
_response(operation, json);
@@ -384,7 +396,6 @@ - (void)sendNow:(BOOL)async
384396
|| err.code == NSURLErrorNotConnectedToInternet
385397
|| err.code == NSURLErrorTimedOut) {
386398
NSLog(@"Leanplum: %@", err);
387-
[LeanplumRequest pushUnsentRequests:requestsToSend];
388399
} else {
389400
id errorResponse = completedOperation.responseJSON;
390401
NSString *errorMessage = [LPResponse getResponseError:[LPResponse getLastResponse:errorResponse]];
@@ -403,6 +414,9 @@ - (void)sendNow:(BOOL)async
403414
} else {
404415
NSLog(@"Leanplum: %@", err);
405416
}
417+
418+
// Delete on permanant error state.
419+
[LPEventDataManager deleteEventsWithLimit:requestsToSend.count];
406420
}
407421
if (_error != nil) {
408422
_error(err);
@@ -442,25 +456,19 @@ - (void)sendEventually
442456
RETURN_IF_TEST_MODE;
443457
if (!_sent) {
444458
_sent = YES;
459+
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
460+
NSString *uuid = [userDefaults objectForKey:LEANPLUM_DEFAULTS_UUID_KEY];
461+
NSInteger count = [LPEventDataManager count];
462+
if (!uuid || count % MAX_ACTIONS_PER_API_CALL == 0) {
463+
uuid = [LeanplumRequest generateUUID];
464+
}
465+
445466
NSMutableDictionary *args = [self createArgsDictionary];
467+
args[LP_PARAM_UUID] = uuid;
446468
[LPEventDataManager addEvent:args];
447469
}
448470
}
449471

450-
+ (void)pushUnsentRequests:(NSArray *)requestData
451-
{
452-
for (NSMutableDictionary *args in requestData) {
453-
NSNumber *retryCount = args[@"retryCount"];
454-
if (!retryCount) {
455-
retryCount = @1;
456-
} else {
457-
retryCount = @([retryCount integerValue] + 1);
458-
}
459-
args[@"retryCount"] = retryCount;
460-
}
461-
[LPEventDataManager addEvents:requestData];
462-
}
463-
464472
+ (NSString *)getSizeAsString:(int)size
465473
{
466474
if (size < (1 << 10)) {

0 commit comments

Comments
 (0)