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,6 +9,7 @@
9 9
 #import <Foundation/Foundation.h>
10 10
 #import "RNFetchBlobProgress.h"
11 11
 #import "RNFetchBlobFS.h"
12
+#import "RNFetchBlobRequest.h"
12 13
 
13 14
 #if __has_include(<React/RCTAssert.h>)
14 15
 #import <React/RCTBridgeModule.h>
@@ -22,8 +23,10 @@
22 23
 
23 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 30
 + (NSMutableDictionary  * _Nullable ) normalizeHeaders:(NSDictionary * _Nullable)headers;
28 31
 + (void) cancelRequest:(NSString * _Nonnull)taskId;
29 32
 + (void) emitExpiredTasks;

+ 10
- 13
ios/RNFetchBlobNetwork.m View File

@@ -13,7 +13,6 @@
13 13
 #import "RNFetchBlob.h"
14 14
 #import "RNFetchBlobConst.h"
15 15
 #import "RNFetchBlobProgress.h"
16
-#import "RNFetchBlobRequest.h"
17 16
 
18 17
 #if __has_include(<React/RCTAssert.h>)
19 18
 #import <React/RCTRootView.h>
@@ -46,23 +45,21 @@ static void initialize_tables() {
46 45
 
47 46
 @implementation RNFetchBlobNetwork
48 47
 
49
-NSOperationQueue *taskQueue;
50
-NSMapTable<NSString*, RNFetchBlobRequest*> * requestsTable;
51 48
 
52 49
 - (id)init {
53 50
     self = [super init];
54 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 59
     return self;
63 60
 }
64 61
 
65
-+ (instancetype)sharedInstance {
62
++ (RNFetchBlobNetwork* _Nullable)sharedInstance {
66 63
     static id _sharedInstance = nil;
67 64
     static dispatch_once_t onceToken;
68 65
     dispatch_once(&onceToken, ^{
@@ -85,11 +82,11 @@ NSMapTable<NSString*, RNFetchBlobRequest*> * requestsTable;
85 82
                   bridge:bridgeRef
86 83
                   taskId:taskId
87 84
              withRequest:req
88
-      taskOperationQueue:taskQueue
85
+      taskOperationQueue:[self sharedInstance].taskQueue
89 86
                 callback:callback];
90 87
     
91 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,7 +94,7 @@ NSMapTable<NSString*, RNFetchBlobRequest*> * requestsTable;
97 94
 {
98 95
     if (config) {
99 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,7 +103,7 @@ NSMapTable<NSString*, RNFetchBlobRequest*> * requestsTable;
106 103
 {
107 104
     if (config) {
108 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,7 +145,7 @@ NSMapTable<NSString*, RNFetchBlobRequest*> * requestsTable;
148 145
     NSURLSessionDataTask * task;
149 146
     
150 147
     @synchronized ([RNFetchBlobNetwork class]) {
151
-        task = [requestsTable objectForKey:taskId].task;
148
+        task = [[self sharedInstance].requestsTable objectForKey:taskId].task;
152 149
     }
153 150
     
154 151
     if(task && task.state == NSURLSessionTaskStateRunning) {