From e8420659e76a198a34822c79d89d1e88b12ddc29 Mon Sep 17 00:00:00 2001 From: YoloMao Date: Fri, 26 Apr 2024 14:38:58 +0800 Subject: [PATCH] fix: events request logic --- .../Event/GrowingEventManager.m | 77 +++++++++---------- 1 file changed, 35 insertions(+), 42 deletions(-) diff --git a/GrowingTrackerCore/Event/GrowingEventManager.m b/GrowingTrackerCore/Event/GrowingEventManager.m index 9f3d1dc8..a4a027f7 100644 --- a/GrowingTrackerCore/Event/GrowingEventManager.m +++ b/GrowingTrackerCore/Event/GrowingEventManager.m @@ -362,48 +362,41 @@ - (void)sendEventsOfChannel_unsafe:(GrowingEventChannel *)channel { channel.isUploading = YES; NSObject *eventRequest = [[GrowingEventRequest alloc] initWithEvents:rawEvents]; - [service - sendRequest:eventRequest - completion:^(NSHTTPURLResponse *_Nonnull httpResponse, NSData *_Nonnull data, NSError *_Nonnull error) { - if (error) { - [GrowingDispatchManager dispatchInGrowingThread:^{ - channel.isUploading = NO; - }]; - } - if ((httpResponse.statusCode >= 200 && httpResponse.statusCode < 400) || httpResponse.statusCode == 413) { - [GrowingDispatchManager dispatchInGrowingThread:^{ - if (isViaCellular) { - if ([eventRequest respondsToSelector:@selector(outsize)]) { - self.uploadEventSize += eventRequest.outsize; - } - } - - if (httpResponse.statusCode != 413) { - for (NSObject *obj in self.allInterceptor) { - if ([obj respondsToSelector:@selector(growingEventManagerEventsDidSend: - request:channel:)]) { - [obj growingEventManagerEventsDidSend:events request:eventRequest channel:channel]; - } - } - } - - [self removeEvents_unsafe:events forChannel:channel]; - channel.isUploading = NO; - - // 如果剩余数量 大于单包数量 则直接发送 - if (channel.db.countOfEvents >= kGrowingMaxBatchSize) { - [self sendEventsInstantWithChannel:channel]; - } - }]; - } else { - [GrowingDispatchManager dispatchInGrowingThread:^{ - if (httpResponse.statusCode == 403) { - [GrowingNetworkPreflight sendPreflight]; - } - channel.isUploading = NO; - }]; - } - }]; + [service sendRequest:eventRequest completion:^(NSHTTPURLResponse *_Nonnull httpResponse, NSData *_Nonnull data, NSError *_Nonnull error) { + [GrowingDispatchManager dispatchInGrowingThread:^{ + channel.isUploading = NO; + + if (error) { + return; + } + + if ((httpResponse.statusCode >= 200 && httpResponse.statusCode < 400) || httpResponse.statusCode == 413) { + if (isViaCellular) { + if ([eventRequest respondsToSelector:@selector(outsize)]) { + self.uploadEventSize += eventRequest.outsize; + } + } + + if (httpResponse.statusCode != 413) { + for (NSObject *obj in self.allInterceptor) { + if ([obj respondsToSelector:@selector(growingEventManagerEventsDidSend: + request:channel:)]) { + [obj growingEventManagerEventsDidSend:events request:eventRequest channel:channel]; + } + } + } + + [self removeEvents_unsafe:events forChannel:channel]; + + // 如果剩余数量 大于单包数量 则直接发送 + if (channel.db.countOfEvents >= kGrowingMaxBatchSize) { + [self sendEventsInstantWithChannel:channel]; + } + } else if (httpResponse.statusCode == 403) { + [GrowingNetworkPreflight sendPreflight]; + } + }]; + }]; } #pragma mark Event Persist