Browse Source

Proper usage of shared instance

Artur Chrusciel 7 years ago
parent
commit
ad65763428
3 changed files with 46 additions and 40 deletions
  1. 15
    15
      ios/RNFetchBlob/RNFetchBlob.m
  2. 10
    4
      ios/RNFetchBlobNetwork.h
  3. 21
    21
      ios/RNFetchBlobNetwork.m

+ 15
- 15
ios/RNFetchBlob/RNFetchBlob.m View File

96
         // send HTTP request
96
         // send HTTP request
97
         else
97
         else
98
         {
98
         {
99
-            [RNFetchBlobNetwork sendRequest:options
100
-                              contentLength:bodyLength
101
-                                     bridge:self.bridge
102
-                                     taskId:taskId
103
-                                withRequest:req
104
-                                   callback:callback];
99
+            [[RNFetchBlobNetwork sharedInstance] sendRequest:options
100
+                                               contentLength:bodyLength
101
+                                                      bridge:self.bridge
102
+                                                      taskId:taskId
103
+                                                 withRequest:req
104
+                                                    callback:callback];
105
         }
105
         }
106
     }];
106
     }];
107
 
107
 
132
         // send HTTP request
132
         // send HTTP request
133
         else
133
         else
134
         {
134
         {
135
-            [RNFetchBlobNetwork sendRequest:options
136
-                              contentLength:bodyLength
137
-                                     bridge:self.bridge
138
-                                     taskId:taskId
139
-                                withRequest:req
140
-                                   callback:callback];
135
+            [[RNFetchBlobNetwork sharedInstance] sendRequest:options
136
+                                               contentLength:bodyLength
137
+                                                      bridge:self.bridge
138
+                                                      taskId:taskId
139
+                                                 withRequest:req
140
+                                                    callback:callback];
141
         }
141
         }
142
     }];
142
     }];
143
 }
143
 }
496
 
496
 
497
 #pragma mark - net.cancelRequest
497
 #pragma mark - net.cancelRequest
498
 RCT_EXPORT_METHOD(cancelRequest:(NSString *)taskId callback:(RCTResponseSenderBlock)callback) {
498
 RCT_EXPORT_METHOD(cancelRequest:(NSString *)taskId callback:(RCTResponseSenderBlock)callback) {
499
-    [RNFetchBlobNetwork cancelRequest:taskId];
499
+    [[RNFetchBlobNetwork sharedInstance] cancelRequest:taskId];
500
     callback(@[[NSNull null], taskId]);
500
     callback(@[[NSNull null], taskId]);
501
 
501
 
502
 }
502
 }
506
 {
506
 {
507
 
507
 
508
     RNFetchBlobProgress * cfg = [[RNFetchBlobProgress alloc] initWithType:Download interval:interval count:count];
508
     RNFetchBlobProgress * cfg = [[RNFetchBlobProgress alloc] initWithType:Download interval:interval count:count];
509
-    [RNFetchBlobNetwork enableProgressReport:taskId config:cfg];
509
+    [[RNFetchBlobNetwork sharedInstance] enableProgressReport:taskId config:cfg];
510
 }
510
 }
511
 
511
 
512
 #pragma mark - net.enableUploadProgressReport
512
 #pragma mark - net.enableUploadProgressReport
513
 RCT_EXPORT_METHOD(enableUploadProgressReport:(NSString *)taskId interval:(nonnull NSNumber*)interval count:(nonnull NSNumber*)count)
513
 RCT_EXPORT_METHOD(enableUploadProgressReport:(NSString *)taskId interval:(nonnull NSNumber*)interval count:(nonnull NSNumber*)count)
514
 {
514
 {
515
     RNFetchBlobProgress * cfg = [[RNFetchBlobProgress alloc] initWithType:Upload interval:interval count:count];
515
     RNFetchBlobProgress * cfg = [[RNFetchBlobProgress alloc] initWithType:Upload interval:interval count:count];
516
-    [RNFetchBlobNetwork enableUploadProgress:taskId config:cfg];
516
+    [[RNFetchBlobNetwork sharedInstance] enableUploadProgress:taskId config:cfg];
517
 }
517
 }
518
 
518
 
519
 #pragma mark - fs.slice
519
 #pragma mark - fs.slice

+ 10
- 4
ios/RNFetchBlobNetwork.h View File

28
 
28
 
29
 + (RNFetchBlobNetwork* _Nullable)sharedInstance;
29
 + (RNFetchBlobNetwork* _Nullable)sharedInstance;
30
 + (NSMutableDictionary  * _Nullable ) normalizeHeaders:(NSDictionary * _Nullable)headers;
30
 + (NSMutableDictionary  * _Nullable ) normalizeHeaders:(NSDictionary * _Nullable)headers;
31
-+ (void) cancelRequest:(NSString * _Nonnull)taskId;
32
 + (void) emitExpiredTasks;
31
 + (void) emitExpiredTasks;
33
-+ (void) enableProgressReport:(NSString * _Nonnull) taskId config:(RNFetchBlobProgress * _Nullable)config;
34
-+ (void) enableUploadProgress:(NSString * _Nonnull) taskId config:(RNFetchBlobProgress * _Nullable)config;
35
 
32
 
36
 - (nullable id) init;
33
 - (nullable id) init;
37
-+ (void) sendRequest:(NSDictionary  * _Nullable )options contentLength:(long)contentLength bridge:(RCTBridge * _Nullable)bridgeRef taskId:(NSString * _Nullable)taskId withRequest:(NSURLRequest * _Nullable)req callback:(_Nullable RCTResponseSenderBlock) callback;
34
+- (void) sendRequest:(NSDictionary  * _Nullable )options
35
+       contentLength:(long)contentLength
36
+              bridge:(RCTBridge * _Nullable)bridgeRef
37
+              taskId:(NSString * _Nullable)taskId
38
+         withRequest:(NSURLRequest * _Nullable)req
39
+            callback:(_Nullable RCTResponseSenderBlock) callback;
40
+- (void) cancelRequest:(NSString * _Nonnull)taskId;
41
+- (void) enableProgressReport:(NSString * _Nonnull) taskId config:(RNFetchBlobProgress * _Nullable)config;
42
+- (void) enableUploadProgress:(NSString * _Nonnull) taskId config:(RNFetchBlobProgress * _Nullable)config;
43
+
38
 
44
 
39
 @end
45
 @end
40
 
46
 

+ 21
- 21
ios/RNFetchBlobNetwork.m View File

69
     return _sharedInstance;
69
     return _sharedInstance;
70
 }
70
 }
71
 
71
 
72
-+ (void) sendRequest:(__weak NSDictionary  * _Nullable )options
72
+- (void) sendRequest:(__weak NSDictionary  * _Nullable )options
73
        contentLength:(long) contentLength
73
        contentLength:(long) contentLength
74
               bridge:(RCTBridge * _Nullable)bridgeRef
74
               bridge:(RCTBridge * _Nullable)bridgeRef
75
               taskId:(NSString * _Nullable)taskId
75
               taskId:(NSString * _Nullable)taskId
82
                   bridge:bridgeRef
82
                   bridge:bridgeRef
83
                   taskId:taskId
83
                   taskId:taskId
84
              withRequest:req
84
              withRequest:req
85
-      taskOperationQueue:[self sharedInstance].taskQueue
85
+      taskOperationQueue:self.taskQueue
86
                 callback:callback];
86
                 callback:callback];
87
     
87
     
88
     @synchronized([RNFetchBlobNetwork class]) {
88
     @synchronized([RNFetchBlobNetwork class]) {
89
-        [[self sharedInstance].requestsTable setObject:request forKey:taskId];
89
+        [self.requestsTable setObject:request forKey:taskId];
90
     }
90
     }
91
 }
91
 }
92
 
92
 
93
-+ (void) enableProgressReport:(NSString *) taskId config:(RNFetchBlobProgress *)config
93
+- (void) enableProgressReport:(NSString *) taskId config:(RNFetchBlobProgress *)config
94
 {
94
 {
95
     if (config) {
95
     if (config) {
96
         @synchronized ([RNFetchBlobNetwork class]) {
96
         @synchronized ([RNFetchBlobNetwork class]) {
97
-            [[self sharedInstance].requestsTable objectForKey:taskId].progressConfig = config;
97
+            [self.requestsTable objectForKey:taskId].progressConfig = config;
98
         }
98
         }
99
     }
99
     }
100
 }
100
 }
101
 
101
 
102
-+ (void) enableUploadProgress:(NSString *) taskId config:(RNFetchBlobProgress *)config
102
+- (void) enableUploadProgress:(NSString *) taskId config:(RNFetchBlobProgress *)config
103
 {
103
 {
104
     if (config) {
104
     if (config) {
105
         @synchronized ([RNFetchBlobNetwork class]) {
105
         @synchronized ([RNFetchBlobNetwork class]) {
106
-            [[self sharedInstance].requestsTable objectForKey:taskId].uploadProgressConfig = config;
106
+            [self.requestsTable objectForKey:taskId].uploadProgressConfig = config;
107
         }
107
         }
108
     }
108
     }
109
 }
109
 }
110
 
110
 
111
+- (void) cancelRequest:(NSString *)taskId
112
+{
113
+    NSURLSessionDataTask * task;
114
+    
115
+    @synchronized ([RNFetchBlobNetwork class]) {
116
+        task = [self.requestsTable objectForKey:taskId].task;
117
+    }
118
+    
119
+    if(task && task.state == NSURLSessionTaskStateRunning) {
120
+        [task cancel];
121
+    }
122
+}
123
+
111
 // removing case from headers
124
 // removing case from headers
112
 + (NSMutableDictionary *) normalizeHeaders:(NSDictionary *)headers
125
 + (NSMutableDictionary *) normalizeHeaders:(NSDictionary *)headers
113
 {
126
 {
115
     for(NSString * key in headers) {
128
     for(NSString * key in headers) {
116
         [mheaders setValue:[headers valueForKey:key] forKey:[key lowercaseString]];
129
         [mheaders setValue:[headers valueForKey:key] forKey:[key lowercaseString]];
117
     }
130
     }
118
-
131
+    
119
     return mheaders;
132
     return mheaders;
120
 }
133
 }
121
 
134
 
140
     }
153
     }
141
 }
154
 }
142
 
155
 
143
-+ (void) cancelRequest:(NSString *)taskId
144
-{
145
-    NSURLSessionDataTask * task;
146
-    
147
-    @synchronized ([RNFetchBlobNetwork class]) {
148
-        task = [[self sharedInstance].requestsTable objectForKey:taskId].task;
149
-    }
150
-    
151
-    if(task && task.state == NSURLSessionTaskStateRunning) {
152
-        [task cancel];
153
-    }
154
-}
155
-
156
 @end
156
 @end