1
1
#import " FileUploader.h"
2
2
@interface FileUploader ()
3
- @property (nonatomic , strong ) NSMutableDictionary * responsesData;
3
+ @property (nonatomic , strong ) NSMutableDictionary <NSString *, NSDate *> *uploadStartTimes;
4
+ @property (nonatomic , strong ) NSMutableDictionary *responsesData;
4
5
@property (nonatomic , strong ) AFURLSessionManager *manager;
5
6
@end
6
7
@@ -20,13 +21,16 @@ -(id)init{
20
21
return nil ;
21
22
[UploadEvent setupStorage ];
22
23
self.responsesData = [[NSMutableDictionary alloc ] init ];
24
+ self.uploadStartTimes = [[NSMutableDictionary alloc ] init ];
23
25
NSURLSessionConfiguration * configuration = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier: [[NSBundle mainBundle ] bundleIdentifier ]];
24
26
configuration.HTTPMaximumConnectionsPerHost = FileUploader.parallelUploadsLimit ;
25
27
configuration.sessionSendsLaunchEvents = NO ;
26
28
self.manager = [[AFURLSessionManager alloc ] initWithSessionConfiguration: configuration];
27
29
__weak FileUploader *weakSelf = self;
28
30
[self .manager setTaskDidCompleteBlock: ^(NSURLSession * _Nonnull session, NSURLSessionTask * _Nonnull task, NSError * _Nullable error) {
29
31
NSString * uploadId = [NSURLProtocol propertyForKey: kUploadUUIDStrPropertyKey inRequest: task.originalRequest];
32
+ NSDate *startTime = weakSelf.uploadStartTimes [uploadId];
33
+ NSTimeInterval duration = [[NSDate date ] timeIntervalSinceDate: startTime];
30
34
NSLog (@" [BackgroundUpload] Task %@ completed with error %@ " , uploadId, error);
31
35
if (!error){
32
36
NSData * serverData = weakSelf.responsesData [@(task.taskIdentifier)];
@@ -36,16 +40,18 @@ -(id)init{
36
40
@" id" : uploadId,
37
41
@" state" : @" UPLOADED" ,
38
42
@" statusCode" : @(((NSHTTPURLResponse *)task.response ).statusCode ),
39
- @" serverResponse" : serverResponse
43
+ @" serverResponse" : serverResponse,
44
+ @" uploadDuration" : @(duration)
40
45
}];
41
46
} else {
42
47
[weakSelf saveAndSendEvent: @{
43
48
@" id" : uploadId,
44
49
@" state" : @" FAILED" ,
45
50
@" error" : error.localizedDescription ,
46
- @" errorCode" : @(error.code )
51
+ @" errorCode" : @(error.code ),
47
52
}];
48
53
}
54
+ [weakSelf.uploadStartTimes removeObjectForKey: uploadId]; // Clean up
49
55
}];
50
56
51
57
[self .manager setDataTaskDidReceiveDataBlock: ^(NSURLSession * _Nonnull session, NSURLSessionDataTask * _Nonnull dataTask, NSData * _Nonnull data) {
@@ -60,7 +66,7 @@ -(id)init{
60
66
}
61
67
62
68
-(void )saveAndSendEvent : (NSDictionary *)data {
63
- UploadEvent*event = [UploadEvent create: data];
69
+ UploadEvent* event = [UploadEvent create: data];
64
70
[self sendEvent: [event dataRepresentation ]];
65
71
}
66
72
@@ -89,6 +95,9 @@ -(void)addUpload:(NSDictionary *)payload completionHandler:(void (^)(NSError* er
89
95
completionHandler: ^(NSError *error, NSMutableURLRequest *request) {
90
96
if (error)
91
97
return handler (error);
98
+
99
+ weakSelf.uploadStartTimes [payload[@" id" ]] = [NSDate date ];
100
+
92
101
__block double lastProgressTimeStamp = 0 ;
93
102
94
103
[[weakSelf.manager uploadTaskWithRequest: request
0 commit comments