Skip to content

Commit

Permalink
fix: events request logic
Browse files Browse the repository at this point in the history
  • Loading branch information
YoloMao committed Apr 26, 2024
1 parent 085e80d commit e842065
Showing 1 changed file with 35 additions and 42 deletions.
77 changes: 35 additions & 42 deletions GrowingTrackerCore/Event/GrowingEventManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -362,48 +362,41 @@ - (void)sendEventsOfChannel_unsafe:(GrowingEventChannel *)channel {

channel.isUploading = YES;
NSObject<GrowingRequestProtocol> *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<GrowingEventInterceptor> *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<GrowingEventInterceptor> *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
Expand Down

0 comments on commit e842065

Please sign in to comment.