Skip to content

Commit ad65763

Browse files
author
Artur Chrusciel
committed
Proper usage of shared instance
1 parent db68875 commit ad65763

File tree

3 files changed

+46
-40
lines changed

3 files changed

+46
-40
lines changed

ios/RNFetchBlob/RNFetchBlob.m

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,12 @@ - (NSDictionary *)constantsToExport
9696
// send HTTP request
9797
else
9898
{
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];
105105
}
106106
}];
107107

@@ -132,12 +132,12 @@ - (NSDictionary *)constantsToExport
132132
// send HTTP request
133133
else
134134
{
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];
141141
}
142142
}];
143143
}
@@ -496,7 +496,7 @@ - (NSDictionary *)constantsToExport
496496

497497
#pragma mark - net.cancelRequest
498498
RCT_EXPORT_METHOD(cancelRequest:(NSString *)taskId callback:(RCTResponseSenderBlock)callback) {
499-
[RNFetchBlobNetwork cancelRequest:taskId];
499+
[[RNFetchBlobNetwork sharedInstance] cancelRequest:taskId];
500500
callback(@[[NSNull null], taskId]);
501501

502502
}
@@ -506,14 +506,14 @@ - (NSDictionary *)constantsToExport
506506
{
507507

508508
RNFetchBlobProgress * cfg = [[RNFetchBlobProgress alloc] initWithType:Download interval:interval count:count];
509-
[RNFetchBlobNetwork enableProgressReport:taskId config:cfg];
509+
[[RNFetchBlobNetwork sharedInstance] enableProgressReport:taskId config:cfg];
510510
}
511511

512512
#pragma mark - net.enableUploadProgressReport
513513
RCT_EXPORT_METHOD(enableUploadProgressReport:(NSString *)taskId interval:(nonnull NSNumber*)interval count:(nonnull NSNumber*)count)
514514
{
515515
RNFetchBlobProgress * cfg = [[RNFetchBlobProgress alloc] initWithType:Upload interval:interval count:count];
516-
[RNFetchBlobNetwork enableUploadProgress:taskId config:cfg];
516+
[[RNFetchBlobNetwork sharedInstance] enableUploadProgress:taskId config:cfg];
517517
}
518518

519519
#pragma mark - fs.slice

ios/RNFetchBlobNetwork.h

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,19 @@
2828

2929
+ (RNFetchBlobNetwork* _Nullable)sharedInstance;
3030
+ (NSMutableDictionary * _Nullable ) normalizeHeaders:(NSDictionary * _Nullable)headers;
31-
+ (void) cancelRequest:(NSString * _Nonnull)taskId;
3231
+ (void) emitExpiredTasks;
33-
+ (void) enableProgressReport:(NSString * _Nonnull) taskId config:(RNFetchBlobProgress * _Nullable)config;
34-
+ (void) enableUploadProgress:(NSString * _Nonnull) taskId config:(RNFetchBlobProgress * _Nullable)config;
3532

3633
- (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+
3844

3945
@end
4046

ios/RNFetchBlobNetwork.m

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ + (RNFetchBlobNetwork* _Nullable)sharedInstance {
6969
return _sharedInstance;
7070
}
7171

72-
+ (void) sendRequest:(__weak NSDictionary * _Nullable )options
72+
- (void) sendRequest:(__weak NSDictionary * _Nullable )options
7373
contentLength:(long) contentLength
7474
bridge:(RCTBridge * _Nullable)bridgeRef
7575
taskId:(NSString * _Nullable)taskId
@@ -82,40 +82,53 @@ + (void) sendRequest:(__weak NSDictionary * _Nullable )options
8282
bridge:bridgeRef
8383
taskId:taskId
8484
withRequest:req
85-
taskOperationQueue:[self sharedInstance].taskQueue
85+
taskOperationQueue:self.taskQueue
8686
callback:callback];
8787

8888
@synchronized([RNFetchBlobNetwork class]) {
89-
[[self sharedInstance].requestsTable setObject:request forKey:taskId];
89+
[self.requestsTable setObject:request forKey:taskId];
9090
}
9191
}
9292

93-
+ (void) enableProgressReport:(NSString *) taskId config:(RNFetchBlobProgress *)config
93+
- (void) enableProgressReport:(NSString *) taskId config:(RNFetchBlobProgress *)config
9494
{
9595
if (config) {
9696
@synchronized ([RNFetchBlobNetwork class]) {
97-
[[self sharedInstance].requestsTable objectForKey:taskId].progressConfig = config;
97+
[self.requestsTable objectForKey:taskId].progressConfig = config;
9898
}
9999
}
100100
}
101101

102-
+ (void) enableUploadProgress:(NSString *) taskId config:(RNFetchBlobProgress *)config
102+
- (void) enableUploadProgress:(NSString *) taskId config:(RNFetchBlobProgress *)config
103103
{
104104
if (config) {
105105
@synchronized ([RNFetchBlobNetwork class]) {
106-
[[self sharedInstance].requestsTable objectForKey:taskId].uploadProgressConfig = config;
106+
[self.requestsTable objectForKey:taskId].uploadProgressConfig = config;
107107
}
108108
}
109109
}
110110

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+
111124
// removing case from headers
112125
+ (NSMutableDictionary *) normalizeHeaders:(NSDictionary *)headers
113126
{
114127
NSMutableDictionary * mheaders = [[NSMutableDictionary alloc]init];
115128
for(NSString * key in headers) {
116129
[mheaders setValue:[headers valueForKey:key] forKey:[key lowercaseString]];
117130
}
118-
131+
119132
return mheaders;
120133
}
121134

@@ -140,17 +153,4 @@ + (void) emitExpiredTasks
140153
}
141154
}
142155

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-
156156
@end

0 commit comments

Comments
 (0)