Skip to content

Commit

Permalink
upload duration logic ios
Browse files Browse the repository at this point in the history
  • Loading branch information
parveshneedhoo committed Aug 20, 2024
1 parent 6e35852 commit db50edb
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/ios/FileUploader.m
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#import "FileUploader.h"
@interface FileUploader()
@property (nonatomic, strong) NSMutableDictionary* responsesData;
@property (nonatomic, strong) NSMutableDictionary<NSString *, NSDate *> *uploadStartTimes;
@property (nonatomic, strong) NSMutableDictionary *responsesData;
@property (nonatomic, strong) AFURLSessionManager *manager;
@end

Expand All @@ -20,13 +21,16 @@ -(id)init{
return nil;
[UploadEvent setupStorage];
self.responsesData = [[NSMutableDictionary alloc] init];
self.uploadStartTimes = [[NSMutableDictionary alloc] init];
NSURLSessionConfiguration* configuration = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:[[NSBundle mainBundle] bundleIdentifier]];
configuration.HTTPMaximumConnectionsPerHost = FileUploader.parallelUploadsLimit;
configuration.sessionSendsLaunchEvents = NO;
self.manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
__weak FileUploader *weakSelf = self;
[self.manager setTaskDidCompleteBlock:^(NSURLSession * _Nonnull session, NSURLSessionTask * _Nonnull task, NSError * _Nullable error) {
NSString* uploadId = [NSURLProtocol propertyForKey:kUploadUUIDStrPropertyKey inRequest:task.originalRequest];
NSDate *startTime = weakSelf.uploadStartTimes[uploadId];
NSTimeInterval duration = [[NSDate date] timeIntervalSinceDate:startTime];
NSLog(@"[BackgroundUpload] Task %@ completed with error %@", uploadId, error);
if (!error){
NSData* serverData = weakSelf.responsesData[@(task.taskIdentifier)];
Expand All @@ -36,16 +40,18 @@ -(id)init{
@"id" : uploadId,
@"state" : @"UPLOADED",
@"statusCode" : @(((NSHTTPURLResponse *)task.response).statusCode),
@"serverResponse" : serverResponse
@"serverResponse" : serverResponse,
@"uploadDuration" : @(duration)
}];
} else {
[weakSelf saveAndSendEvent:@{
@"id" : uploadId,
@"state" : @"FAILED",
@"error" : error.localizedDescription,
@"errorCode" : @(error.code)
@"errorCode" : @(error.code),
}];
}
[weakSelf.uploadStartTimes removeObjectForKey:uploadId]; // Clean up
}];

[self.manager setDataTaskDidReceiveDataBlock:^(NSURLSession * _Nonnull session, NSURLSessionDataTask * _Nonnull dataTask, NSData * _Nonnull data) {
Expand All @@ -60,7 +66,7 @@ -(id)init{
}

-(void)saveAndSendEvent:(NSDictionary*)data{
UploadEvent*event = [UploadEvent create:data];
UploadEvent* event = [UploadEvent create:data];
[self sendEvent:[event dataRepresentation]];
}

Expand Down Expand Up @@ -89,6 +95,9 @@ -(void)addUpload:(NSDictionary *)payload completionHandler:(void (^)(NSError* er
completionHandler:^(NSError *error, NSMutableURLRequest *request) {
if (error)
return handler(error);

weakSelf.uploadStartTimes[payload[@"id"]] = [NSDate date];

__block double lastProgressTimeStamp = 0;

[[weakSelf.manager uploadTaskWithRequest:request
Expand Down

0 comments on commit db50edb

Please sign in to comment.