Skip to content

Commit 3cf1f62

Browse files
authored
batch request splitting and batching timer (#413)
* split batching requests * add method in Leanplum to set batching timer (timer to periodically upload events) * fix headers for spm
1 parent 5d7b5bd commit 3cf1f62

19 files changed

+249
-33
lines changed

Example/Tests/Classes/LPEventDataManagerTest.m

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#import "LeanplumReachability+Category.h"
3939
#import "LPJSON.h"
4040
#import "LPOperationQueue.h"
41+
#import "LPNetworkConstants.h"
4142

4243
@interface LPEventDataManager(UnitTest)
4344

@@ -196,13 +197,13 @@ - (void)test_uuid
196197
XCTAssertTrue(events.count == 0);
197198

198199
// UUID should be different after the 10k mark.
199-
for (int i=0; i<MAX_EVENTS_PER_API_CALL+1; i++) {
200+
for (int i=0; i<LP_MAX_EVENTS_PER_API_CALL+1; i++) {
200201
[Leanplum track:@"s"];
201202
}
202203
[[LPOperationQueue serialQueue] waitUntilAllOperationsAreFinished];
203204
events = [LPEventDataManager eventsWithLimit:900000];
204-
XCTAssertTrue(events.count == MAX_EVENTS_PER_API_CALL+1);
205-
XCTAssertFalse([events[0][@"uuid"] isEqual:events[MAX_EVENTS_PER_API_CALL][@"uuid"]]);
205+
XCTAssertTrue(events.count == LP_MAX_EVENTS_PER_API_CALL+1);
206+
XCTAssertFalse([events[0][@"uuid"] isEqual:events[LP_MAX_EVENTS_PER_API_CALL][@"uuid"]]);
206207

207208
// Make sure there will be 2 requests that are split because of MAX_EVENTS_PER_API_CALL.
208209
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);

Leanplum-SDK.xcodeproj/project.pbxproj

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,12 @@
150150
C563DF2824573DE4D91EDFEE /* LPConstants.m in Sources */ = {isa = PBXBuildFile; fileRef = 004B70DF819F0BDDF8F08873 /* LPConstants.m */; };
151151
C56A84A441B9C5056A0F3CFB /* LPOperationQueue.h in Headers */ = {isa = PBXBuildFile; fileRef = DED1657DCA0BEE92D31DE22D /* LPOperationQueue.h */; settings = {ATTRIBUTES = (Public, ); }; };
152152
C722BB509AEC55EC813B93BF /* Leanplum_SocketIO.h in Headers */ = {isa = PBXBuildFile; fileRef = FE8B81378775839CDC9FF7C0 /* Leanplum_SocketIO.h */; settings = {ATTRIBUTES = (Public, ); }; };
153+
C97707F6256F228A007AAB3A /* LPRequestBatch.h in Headers */ = {isa = PBXBuildFile; fileRef = C97707F2256F2289007AAB3A /* LPRequestBatch.h */; };
154+
C97707F7256F228A007AAB3A /* LPRequestBatchFactory.h in Headers */ = {isa = PBXBuildFile; fileRef = C97707F3256F228A007AAB3A /* LPRequestBatchFactory.h */; };
155+
C97707F8256F228A007AAB3A /* LPRequestBatchFactory.m in Sources */ = {isa = PBXBuildFile; fileRef = C97707F4256F228A007AAB3A /* LPRequestBatchFactory.m */; };
156+
C97707F9256F228A007AAB3A /* LPRequestBatch.m in Sources */ = {isa = PBXBuildFile; fileRef = C97707F5256F228A007AAB3A /* LPRequestBatch.m */; };
157+
C9770805256F96BA007AAB3A /* LPRequestUUIDHelper.h in Headers */ = {isa = PBXBuildFile; fileRef = C9770803256F96BA007AAB3A /* LPRequestUUIDHelper.h */; };
158+
C9770806256F96BA007AAB3A /* LPRequestUUIDHelper.m in Sources */ = {isa = PBXBuildFile; fileRef = C9770804256F96BA007AAB3A /* LPRequestUUIDHelper.m */; };
153159
C99A38FB2523503B006A9A8F /* LPRequestSenderTimer.h in Headers */ = {isa = PBXBuildFile; fileRef = C99A38F92523503B006A9A8F /* LPRequestSenderTimer.h */; settings = {ATTRIBUTES = (Public, ); }; };
154160
C99A38FC2523503B006A9A8F /* LPRequestSenderTimer.m in Sources */ = {isa = PBXBuildFile; fileRef = C99A38FA2523503B006A9A8F /* LPRequestSenderTimer.m */; };
155161
CAB620162C4A4A0D411A7484 /* LPWebInterstitialViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = E4E7A92BCE3E37B364E01D9B /* LPWebInterstitialViewController.m */; };
@@ -323,6 +329,12 @@
323329
C2B6A5CBB11B15F1D1210155 /* LPAppIconManager.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = LPAppIconManager.h; sourceTree = "<group>"; };
324330
C2CBACF77D114F87E168C2D2 /* LPFileTransferManager.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = LPFileTransferManager.m; sourceTree = "<group>"; };
325331
C3FEC7ED2881D8A73E28C303 /* LPPushAskToAskMessageTemplate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = LPPushAskToAskMessageTemplate.h; sourceTree = "<group>"; };
332+
C97707F2256F2289007AAB3A /* LPRequestBatch.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LPRequestBatch.h; sourceTree = "<group>"; };
333+
C97707F3256F228A007AAB3A /* LPRequestBatchFactory.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LPRequestBatchFactory.h; sourceTree = "<group>"; };
334+
C97707F4256F228A007AAB3A /* LPRequestBatchFactory.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = LPRequestBatchFactory.m; sourceTree = "<group>"; };
335+
C97707F5256F228A007AAB3A /* LPRequestBatch.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = LPRequestBatch.m; sourceTree = "<group>"; };
336+
C9770803256F96BA007AAB3A /* LPRequestUUIDHelper.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LPRequestUUIDHelper.h; sourceTree = "<group>"; };
337+
C9770804256F96BA007AAB3A /* LPRequestUUIDHelper.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = LPRequestUUIDHelper.m; sourceTree = "<group>"; };
326338
C99A38F92523503B006A9A8F /* LPRequestSenderTimer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LPRequestSenderTimer.h; sourceTree = "<group>"; };
327339
C99A38FA2523503B006A9A8F /* LPRequestSenderTimer.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = LPRequestSenderTimer.m; sourceTree = "<group>"; };
328340
CB8BE7E9A55BB285B29723E1 /* LPVarCache.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = LPVarCache.m; sourceTree = "<group>"; };
@@ -627,6 +639,12 @@
627639
5DE08554B0E37BE700C20DAA /* LPNetworkProtocol.h */,
628640
5C65EB9556E41FC23647AFFC /* LPRequest.h */,
629641
2553D7685D3F6A7735C06CE4 /* LPRequest.m */,
642+
C97707F2256F2289007AAB3A /* LPRequestBatch.h */,
643+
C97707F5256F228A007AAB3A /* LPRequestBatch.m */,
644+
C97707F3256F228A007AAB3A /* LPRequestBatchFactory.h */,
645+
C97707F4256F228A007AAB3A /* LPRequestBatchFactory.m */,
646+
C9770803256F96BA007AAB3A /* LPRequestUUIDHelper.h */,
647+
C9770804256F96BA007AAB3A /* LPRequestUUIDHelper.m */,
630648
19AE62BF2557D53314A72975 /* LPRequestFactory.h */,
631649
36133E96E82A8370E9E9393A /* LPRequestFactory.m */,
632650
633C71E88C86E1751C969974 /* LPRequestSender.h */,
@@ -817,6 +835,8 @@
817835
78ACFA2AB35973CEB067F3EC /* LPActionContext-Internal.h in Headers */,
818836
D4E45606379C4C1EDB175C04 /* LPActionContext.h in Headers */,
819837
38FE424BA40ECF063A4B0D77 /* LPActionManager.h in Headers */,
838+
C9770805256F96BA007AAB3A /* LPRequestUUIDHelper.h in Headers */,
839+
C97707F6256F228A007AAB3A /* LPRequestBatch.h in Headers */,
820840
34E54F6B0F56F902C2AC8145 /* LPAlertMessageTemplate.h in Headers */,
821841
08051C2E3C76FB5DFD7334CC /* LPAppIconManager.h in Headers */,
822842
A456BBE6B7B8BBDCE6E015A7 /* LPAppRatingMessageTemplate.h in Headers */,
@@ -862,6 +882,7 @@
862882
1056E4D914F41026D49870F5 /* LPRequest.h in Headers */,
863883
3E416D0B2C86C841A8B9E2DE /* LPRequestFactory.h in Headers */,
864884
CB24750D7C2F9AFCC751D269 /* LPRequestSender.h in Headers */,
885+
C97707F7256F228A007AAB3A /* LPRequestBatchFactory.h in Headers */,
865886
8B473962853F7E1D5F3DDC78 /* LPResponse.h in Headers */,
866887
29E2700BAFB249E6CDBA28E4 /* LPRevenueManager.h in Headers */,
867888
619B024D57210214484C6784 /* LPRichInterstitialMessageTemplate.h in Headers */,
@@ -990,6 +1011,7 @@
9901011
isa = PBXSourcesBuildPhase;
9911012
buildActionMask = 2147483647;
9921013
files = (
1014+
C9770806256F96BA007AAB3A /* LPRequestUUIDHelper.m in Sources */,
9931015
DB2A7929F4CE0AC3110609B3 /* FileMD5Hash.c in Sources */,
9941016
16E5B0F75A12F34C3E94D055 /* LPAES.m in Sources */,
9951017
8790C8CFF2F4C592052EAE24 /* LPAPIConfig.m in Sources */,
@@ -1001,6 +1023,7 @@
10011023
3D220BF1DB5E5B9449F98A29 /* LPAppIconManager.m in Sources */,
10021024
2CA501F8369D9EC6A53685ED /* LPAppRatingMessageTemplate.m in Sources */,
10031025
BCB9CA479AF9BA2BF8B059C0 /* LPCenterPopupMessageTemplate.m in Sources */,
1026+
C97707F8256F228A007AAB3A /* LPRequestBatchFactory.m in Sources */,
10041027
8F60CFA5DDF2507642C24414 /* LPConfirmMessageTemplate.m in Sources */,
10051028
C563DF2824573DE4D91EDFEE /* LPConstants.m in Sources */,
10061029
3704BEBBC6C93D2DF74C3613 /* LPContextualValues.m in Sources */,
@@ -1040,6 +1063,7 @@
10401063
875717F047C2EC1893A0CBFE /* LPRegisterForPushMessageTemplate.m in Sources */,
10411064
3ED83826236AD245F151C833 /* LPRequest.m in Sources */,
10421065
D2725B4A7121B718F97FD0BA /* LPRequestFactory.m in Sources */,
1066+
C97707F9256F228A007AAB3A /* LPRequestBatch.m in Sources */,
10431067
927DBF55A45D29F9CFE26E4D /* LPRequestSender.m in Sources */,
10441068
82CA232EB86495333009CCFE /* LPResponse.m in Sources */,
10451069
61F094D2E0A16F52CAA35D46 /* LPRevenueManager.m in Sources */,

Leanplum-SDK/Classes/Internal/LPConstants.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,7 @@
4747
#define MAX_UPLOAD_BATCH_FILES 100
4848
#define MAX_FILES_SUPPORTED 1000
4949

50-
#define HEARTBEAT_INTERVAL 15 * 60 // 15 minutes
51-
5250
#define MAX_STORED_OCCURRENCES_PER_MESSAGE 100
53-
#define MAX_EVENTS_PER_API_CALL 10000
5451

5552
#define DEFAULT_PRIORITY 1000
5653

@@ -141,7 +138,6 @@ OBJC_EXPORT NSString *LEANPLUM_DEFAULTS_PRE_LEANPLUM_INSTALL_KEY;
141138
OBJC_EXPORT NSString *LEANPLUM_DEFAULTS_SDK_VERSION;
142139
OBJC_EXPORT NSString *LEANPLUM_DEFAULTS_INBOX_KEY;
143140
OBJC_EXPORT NSString *LEANPLUM_DEFAULTS_APP_VERSION_KEY;
144-
OBJC_EXPORT NSString *LEANPLUM_DEFAULTS_UUID_KEY;
145141

146142
OBJC_EXPORT NSString *LEANPLUM_SQLITE_NAME;
147143

Leanplum-SDK/Classes/Internal/LPConstants.m

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ - (id)init {
9090
NSString *LEANPLUM_DEFAULTS_SDK_VERSION = @"__leanplum_version";
9191
NSString *LEANPLUM_DEFAULTS_INBOX_KEY = @"__leanplum_newsfeed";
9292
NSString *LEANPLUM_DEFAULTS_APP_VERSION_KEY = @"leanplum_savedAppVersionKey";
93-
NSString *LEANPLUM_DEFAULTS_UUID_KEY = @"__leanplum_uuid";
9493

9594
NSString *LEANPLUM_SQLITE_NAME = @"__leanplum.sqlite";
9695

Leanplum-SDK/Classes/Internal/Leanplum.m

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2762,6 +2762,11 @@ + (NSDictionary *)parseFileURLsFromResponse:(NSDictionary *)response {
27622762
return nil;
27632763
}
27642764

2765+
+ (void)setEventsUploadInterval:(LPEventsUploadInterval)uploadInterval
2766+
{
2767+
[[LPRequestSenderTimer sharedInstance] setTimerInterval:uploadInterval];
2768+
}
2769+
27652770
- (void) dealloc {
27662771
[[NSNotificationCenter defaultCenter] removeObserver:self];
27672772
}

Leanplum-SDK/Classes/Leanplum.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
#import "LPNetworkEngine.h"
6767
#import "LPAES.h"
6868
#import "LPLogManager.h"
69+
#import "LPRequestSenderTimer.h"
6970

7071
// Prevent circular reference
7172
@class LPDeferrableAction;
@@ -847,6 +848,12 @@ NS_SWIFT_NAME(setDeviceLocation(latitude:longitude:city:region:country:type:));
847848
*/
848849
+ (void)disableLocationCollection;
849850

851+
/**
852+
Sets the time interval to periodically upload events to server.
853+
Default is 15 minutes.
854+
@param uploadInterval The time between uploads.
855+
*/
856+
+ (void)setEventsUploadInterval:(LPEventsUploadInterval)uploadInterval;
850857
@end
851858

852859
NS_ASSUME_NONNULL_END

Leanplum-SDK/Classes/Managers/Networking/LPNetworkConstants.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,4 @@ static NSString *LP_API_METHOD_LOG = @"log";
3939
static NSString *LP_API_METHOD_GET_INBOX_MESSAGES = @"getNewsfeedMessages";
4040
static NSString *LP_API_METHOD_MARK_INBOX_MESSAGE_AS_READ = @"markNewsfeedMessageAsRead";
4141
static NSString *LP_API_METHOD_DELETE_INBOX_MESSAGE = @"deleteNewsfeedMessage";
42+
static int LP_MAX_EVENTS_PER_API_CALL = 10000;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//
2+
// LPRequestBatch.h
3+
// Leanplum-iOS-SDK
4+
//
5+
// Copyright © 2020 Leanplum. All rights reserved.
6+
//
7+
8+
#import <Foundation/Foundation.h>
9+
10+
NS_ASSUME_NONNULL_BEGIN
11+
12+
@interface LPRequestBatch : NSObject
13+
14+
@property (nonatomic, retain) NSArray *requestsToSend;
15+
16+
- (instancetype)initWithRequestsToSend:(NSArray *)requestsToSend;
17+
- (int)getEventsCount;
18+
- (BOOL)isFull;
19+
- (BOOL)isEmpty;
20+
21+
@end
22+
23+
NS_ASSUME_NONNULL_END
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//
2+
// LPRequestBatch.m
3+
// Leanplum-iOS-SDK
4+
//
5+
// Copyright © 2020 Leanplum. All rights reserved.
6+
//
7+
8+
#import "LPRequestBatch.h"
9+
#import "LPNetworkConstants.h"
10+
11+
@implementation LPRequestBatch
12+
13+
14+
- (instancetype)initWithRequestsToSend:(NSArray *)requestsToSend
15+
{
16+
if (self = [super init])
17+
{
18+
_requestsToSend = requestsToSend;
19+
}
20+
21+
return self;
22+
}
23+
24+
- (int)getEventsCount
25+
{
26+
return (int)self.requestsToSend.count;
27+
}
28+
29+
- (BOOL)isFull
30+
{
31+
return [self getEventsCount] == LP_MAX_EVENTS_PER_API_CALL;
32+
}
33+
34+
- (BOOL)isEmpty
35+
{
36+
return self.requestsToSend.count == 0;
37+
}
38+
39+
40+
@end
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//
2+
// LPRequestBatchFactory.h
3+
// Leanplum-iOS-SDK
4+
//
5+
// Copyright © 2020 Leanplum. All rights reserved.
6+
//
7+
8+
#import <Foundation/Foundation.h>
9+
#import "LPRequestBatch.h"
10+
11+
NS_ASSUME_NONNULL_BEGIN
12+
13+
@interface LPRequestBatchFactory : NSObject
14+
15+
+ (LPRequestBatch *)createNextBatch;
16+
+ (void)deleteFinishedBatch:(LPRequestBatch *)batch;
17+
18+
@end
19+
20+
NS_ASSUME_NONNULL_END

0 commit comments

Comments
 (0)