Skip to content

Commit db50edb

Browse files
upload duration logic ios
1 parent 6e35852 commit db50edb

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

src/ios/FileUploader.m

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#import "FileUploader.h"
22
@interface FileUploader()
3-
@property (nonatomic, strong) NSMutableDictionary* responsesData;
3+
@property (nonatomic, strong) NSMutableDictionary<NSString *, NSDate *> *uploadStartTimes;
4+
@property (nonatomic, strong) NSMutableDictionary *responsesData;
45
@property (nonatomic, strong) AFURLSessionManager *manager;
56
@end
67

@@ -20,13 +21,16 @@ -(id)init{
2021
return nil;
2122
[UploadEvent setupStorage];
2223
self.responsesData = [[NSMutableDictionary alloc] init];
24+
self.uploadStartTimes = [[NSMutableDictionary alloc] init];
2325
NSURLSessionConfiguration* configuration = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:[[NSBundle mainBundle] bundleIdentifier]];
2426
configuration.HTTPMaximumConnectionsPerHost = FileUploader.parallelUploadsLimit;
2527
configuration.sessionSendsLaunchEvents = NO;
2628
self.manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
2729
__weak FileUploader *weakSelf = self;
2830
[self.manager setTaskDidCompleteBlock:^(NSURLSession * _Nonnull session, NSURLSessionTask * _Nonnull task, NSError * _Nullable error) {
2931
NSString* uploadId = [NSURLProtocol propertyForKey:kUploadUUIDStrPropertyKey inRequest:task.originalRequest];
32+
NSDate *startTime = weakSelf.uploadStartTimes[uploadId];
33+
NSTimeInterval duration = [[NSDate date] timeIntervalSinceDate:startTime];
3034
NSLog(@"[BackgroundUpload] Task %@ completed with error %@", uploadId, error);
3135
if (!error){
3236
NSData* serverData = weakSelf.responsesData[@(task.taskIdentifier)];
@@ -36,16 +40,18 @@ -(id)init{
3640
@"id" : uploadId,
3741
@"state" : @"UPLOADED",
3842
@"statusCode" : @(((NSHTTPURLResponse *)task.response).statusCode),
39-
@"serverResponse" : serverResponse
43+
@"serverResponse" : serverResponse,
44+
@"uploadDuration" : @(duration)
4045
}];
4146
} else {
4247
[weakSelf saveAndSendEvent:@{
4348
@"id" : uploadId,
4449
@"state" : @"FAILED",
4550
@"error" : error.localizedDescription,
46-
@"errorCode" : @(error.code)
51+
@"errorCode" : @(error.code),
4752
}];
4853
}
54+
[weakSelf.uploadStartTimes removeObjectForKey:uploadId]; // Clean up
4955
}];
5056

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

6268
-(void)saveAndSendEvent:(NSDictionary*)data{
63-
UploadEvent*event = [UploadEvent create:data];
69+
UploadEvent* event = [UploadEvent create:data];
6470
[self sendEvent:[event dataRepresentation]];
6571
}
6672

@@ -89,6 +95,9 @@ -(void)addUpload:(NSDictionary *)payload completionHandler:(void (^)(NSError* er
8995
completionHandler:^(NSError *error, NSMutableURLRequest *request) {
9096
if (error)
9197
return handler(error);
98+
99+
weakSelf.uploadStartTimes[payload[@"id"]] = [NSDate date];
100+
92101
__block double lastProgressTimeStamp = 0;
93102

94103
[[weakSelf.manager uploadTaskWithRequest:request

0 commit comments

Comments
 (0)