Browse Source

Shared instance used

Artur Chrusciel 7 years ago
parent
commit
db68875146
2 changed files with 14 additions and 14 deletions
  1. 4
    1
      ios/RNFetchBlobNetwork.h
  2. 10
    13
      ios/RNFetchBlobNetwork.m

+ 4
- 1
ios/RNFetchBlobNetwork.h View File

9
 #import <Foundation/Foundation.h>
9
 #import <Foundation/Foundation.h>
10
 #import "RNFetchBlobProgress.h"
10
 #import "RNFetchBlobProgress.h"
11
 #import "RNFetchBlobFS.h"
11
 #import "RNFetchBlobFS.h"
12
+#import "RNFetchBlobRequest.h"
12
 
13
 
13
 #if __has_include(<React/RCTAssert.h>)
14
 #if __has_include(<React/RCTAssert.h>)
14
 #import <React/RCTBridgeModule.h>
15
 #import <React/RCTBridgeModule.h>
22
 
23
 
23
 @interface RNFetchBlobNetwork : NSObject  <NSURLSessionDelegate, NSURLSessionTaskDelegate, NSURLSessionDataDelegate>
24
 @interface RNFetchBlobNetwork : NSObject  <NSURLSessionDelegate, NSURLSessionTaskDelegate, NSURLSessionDataDelegate>
24
 
25
 
26
+@property(nonnull, nonatomic) NSOperationQueue *taskQueue;
27
+@property(nonnull, nonatomic) NSMapTable<NSString*, RNFetchBlobRequest*> * requestsTable;
25
 
28
 
26
-+ (_Nullable instancetype)sharedInstance;
29
++ (RNFetchBlobNetwork* _Nullable)sharedInstance;
27
 + (NSMutableDictionary  * _Nullable ) normalizeHeaders:(NSDictionary * _Nullable)headers;
30
 + (NSMutableDictionary  * _Nullable ) normalizeHeaders:(NSDictionary * _Nullable)headers;
28
 + (void) cancelRequest:(NSString * _Nonnull)taskId;
31
 + (void) cancelRequest:(NSString * _Nonnull)taskId;
29
 + (void) emitExpiredTasks;
32
 + (void) emitExpiredTasks;

+ 10
- 13
ios/RNFetchBlobNetwork.m View File

13
 #import "RNFetchBlob.h"
13
 #import "RNFetchBlob.h"
14
 #import "RNFetchBlobConst.h"
14
 #import "RNFetchBlobConst.h"
15
 #import "RNFetchBlobProgress.h"
15
 #import "RNFetchBlobProgress.h"
16
-#import "RNFetchBlobRequest.h"
17
 
16
 
18
 #if __has_include(<React/RCTAssert.h>)
17
 #if __has_include(<React/RCTAssert.h>)
19
 #import <React/RCTRootView.h>
18
 #import <React/RCTRootView.h>
46
 
45
 
47
 @implementation RNFetchBlobNetwork
46
 @implementation RNFetchBlobNetwork
48
 
47
 
49
-NSOperationQueue *taskQueue;
50
-NSMapTable<NSString*, RNFetchBlobRequest*> * requestsTable;
51
 
48
 
52
 - (id)init {
49
 - (id)init {
53
     self = [super init];
50
     self = [super init];
54
     if (self) {
51
     if (self) {
55
-        requestsTable = [NSMapTable mapTableWithKeyOptions:NSMapTableStrongMemory valueOptions:NSMapTableWeakMemory];
52
+        self.requestsTable = [NSMapTable mapTableWithKeyOptions:NSMapTableStrongMemory valueOptions:NSMapTableWeakMemory];
56
         
53
         
57
-        taskQueue = [[NSOperationQueue alloc] init];
58
-        taskQueue.qualityOfService = NSQualityOfServiceUtility;
59
-        taskQueue.maxConcurrentOperationCount = 10;
54
+        self.taskQueue = [[NSOperationQueue alloc] init];
55
+        self.taskQueue.qualityOfService = NSQualityOfServiceUtility;
56
+        self.taskQueue.maxConcurrentOperationCount = 10;
60
     }
57
     }
61
     
58
     
62
     return self;
59
     return self;
63
 }
60
 }
64
 
61
 
65
-+ (instancetype)sharedInstance {
62
++ (RNFetchBlobNetwork* _Nullable)sharedInstance {
66
     static id _sharedInstance = nil;
63
     static id _sharedInstance = nil;
67
     static dispatch_once_t onceToken;
64
     static dispatch_once_t onceToken;
68
     dispatch_once(&onceToken, ^{
65
     dispatch_once(&onceToken, ^{
85
                   bridge:bridgeRef
82
                   bridge:bridgeRef
86
                   taskId:taskId
83
                   taskId:taskId
87
              withRequest:req
84
              withRequest:req
88
-      taskOperationQueue:taskQueue
85
+      taskOperationQueue:[self sharedInstance].taskQueue
89
                 callback:callback];
86
                 callback:callback];
90
     
87
     
91
     @synchronized([RNFetchBlobNetwork class]) {
88
     @synchronized([RNFetchBlobNetwork class]) {
92
-        [requestsTable setObject:request forKey:taskId];
89
+        [[self sharedInstance].requestsTable setObject:request forKey:taskId];
93
     }
90
     }
94
 }
91
 }
95
 
92
 
97
 {
94
 {
98
     if (config) {
95
     if (config) {
99
         @synchronized ([RNFetchBlobNetwork class]) {
96
         @synchronized ([RNFetchBlobNetwork class]) {
100
-            [requestsTable objectForKey:taskId].progressConfig = config;
97
+            [[self sharedInstance].requestsTable objectForKey:taskId].progressConfig = config;
101
         }
98
         }
102
     }
99
     }
103
 }
100
 }
106
 {
103
 {
107
     if (config) {
104
     if (config) {
108
         @synchronized ([RNFetchBlobNetwork class]) {
105
         @synchronized ([RNFetchBlobNetwork class]) {
109
-            [requestsTable objectForKey:taskId].uploadProgressConfig = config;
106
+            [[self sharedInstance].requestsTable objectForKey:taskId].uploadProgressConfig = config;
110
         }
107
         }
111
     }
108
     }
112
 }
109
 }
148
     NSURLSessionDataTask * task;
145
     NSURLSessionDataTask * task;
149
     
146
     
150
     @synchronized ([RNFetchBlobNetwork class]) {
147
     @synchronized ([RNFetchBlobNetwork class]) {
151
-        task = [requestsTable objectForKey:taskId].task;
148
+        task = [[self sharedInstance].requestsTable objectForKey:taskId].task;
152
     }
149
     }
153
     
150
     
154
     if(task && task.state == NSURLSessionTaskStateRunning) {
151
     if(task && task.state == NSURLSessionTaskStateRunning) {