Browse Source

Proper usage of shared instance

Artur Chrusciel 6 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,12 +96,12 @@ RCT_EXPORT_METHOD(fetchBlobForm:(NSDictionary *)options
96 96
         // send HTTP request
97 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,12 +132,12 @@ RCT_EXPORT_METHOD(fetchBlob:(NSDictionary *)options
132 132
         // send HTTP request
133 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,7 +496,7 @@ RCT_EXPORT_METHOD(getEnvironmentDirs:(RCTResponseSenderBlock) callback)
496 496
 
497 497
 #pragma mark - net.cancelRequest
498 498
 RCT_EXPORT_METHOD(cancelRequest:(NSString *)taskId callback:(RCTResponseSenderBlock)callback) {
499
-    [RNFetchBlobNetwork cancelRequest:taskId];
499
+    [[RNFetchBlobNetwork sharedInstance] cancelRequest:taskId];
500 500
     callback(@[[NSNull null], taskId]);
501 501
 
502 502
 }
@@ -506,14 +506,14 @@ RCT_EXPORT_METHOD(enableProgressReport:(NSString *)taskId interval:(nonnull NSNu
506 506
 {
507 507
 
508 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 512
 #pragma mark - net.enableUploadProgressReport
513 513
 RCT_EXPORT_METHOD(enableUploadProgressReport:(NSString *)taskId interval:(nonnull NSNumber*)interval count:(nonnull NSNumber*)count)
514 514
 {
515 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 519
 #pragma mark - fs.slice

+ 10
- 4
ios/RNFetchBlobNetwork.h View File

@@ -28,13 +28,19 @@
28 28
 
29 29
 + (RNFetchBlobNetwork* _Nullable)sharedInstance;
30 30
 + (NSMutableDictionary  * _Nullable ) normalizeHeaders:(NSDictionary * _Nullable)headers;
31
-+ (void) cancelRequest:(NSString * _Nonnull)taskId;
32 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 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 45
 @end
40 46
 

+ 21
- 21
ios/RNFetchBlobNetwork.m View File

@@ -69,7 +69,7 @@ static void initialize_tables() {
69 69
     return _sharedInstance;
70 70
 }
71 71
 
72
-+ (void) sendRequest:(__weak NSDictionary  * _Nullable )options
72
+- (void) sendRequest:(__weak NSDictionary  * _Nullable )options
73 73
        contentLength:(long) contentLength
74 74
               bridge:(RCTBridge * _Nullable)bridgeRef
75 75
               taskId:(NSString * _Nullable)taskId
@@ -82,32 +82,45 @@ static void initialize_tables() {
82 82
                   bridge:bridgeRef
83 83
                   taskId:taskId
84 84
              withRequest:req
85
-      taskOperationQueue:[self sharedInstance].taskQueue
85
+      taskOperationQueue:self.taskQueue
86 86
                 callback:callback];
87 87
     
88 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 95
     if (config) {
96 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 104
     if (config) {
105 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 124
 // removing case from headers
112 125
 + (NSMutableDictionary *) normalizeHeaders:(NSDictionary *)headers
113 126
 {
@@ -115,7 +128,7 @@ static void initialize_tables() {
115 128
     for(NSString * key in headers) {
116 129
         [mheaders setValue:[headers valueForKey:key] forKey:[key lowercaseString]];
117 130
     }
118
-
131
+    
119 132
     return mheaders;
120 133
 }
121 134
 
@@ -140,17 +153,4 @@ static void initialize_tables() {
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 156
 @end