@@ -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