Browse Source

ios code refactor

Ben Hsieh 8 years ago
parent
commit
11a173bd21
2 changed files with 26 additions and 26 deletions
  1. 7
    16
      src/ios/RNFetchBlob/RNFetchBlob.m
  2. 19
    10
      src/ios/RNFetchBlobNetwork.m

+ 7
- 16
src/ios/RNFetchBlob/RNFetchBlob.m View File

81
                   body:(NSString *)body callback:(RCTResponseSenderBlock)callback)
81
                   body:(NSString *)body callback:(RCTResponseSenderBlock)callback)
82
 {
82
 {
83
     NSString *cType = [headers valueForKey:@"content-type"];
83
     NSString *cType = [headers valueForKey:@"content-type"];
84
-//	if (cType != nil && cType == @"application/x-www-form-urlencoded") {
85
-//		[RNFetchBlobReqBuilder buildEncodedRequest:options taskId:taskId method:method url:url headers:headers body:body onComplete:^(NSURLRequest *req, long bodyLength) {
86
-//	        // send HTTP request
87
-//	        RNFetchBlobNetwork * utils = [[RNFetchBlobNetwork alloc] init];
88
-//	        [utils sendRequest:options contentLength:bodyLength bridge:self.bridge taskId:taskId withRequest:req callback:callback];
89
-//	        utils = nil;
90
-//	    }];
91
-//	} else {
92
-		[RNFetchBlobReqBuilder buildOctetRequest:options taskId:taskId method:method url:url headers:headers body:body onComplete:^(NSURLRequest *req, long bodyLength) {
93
-	        // send HTTP request
94
-	        RNFetchBlobNetwork * utils = [[RNFetchBlobNetwork alloc] init];
95
-	        [utils sendRequest:options contentLength:bodyLength bridge:self.bridge taskId:taskId withRequest:req callback:callback];
96
-	        utils = nil;
97
-	    }];
98
-//	}
84
+    
85
+    [RNFetchBlobReqBuilder buildOctetRequest:options taskId:taskId method:method url:url headers:headers body:body onComplete:^(NSURLRequest *req, long bodyLength) {
86
+        // send HTTP request
87
+        RNFetchBlobNetwork * utils = [[RNFetchBlobNetwork alloc] init];
88
+        [utils sendRequest:options contentLength:bodyLength bridge:self.bridge taskId:taskId withRequest:req callback:callback];
89
+        utils = nil;
90
+    }];
99
 }
91
 }
100
 
92
 
101
 RCT_EXPORT_METHOD(createFile:(NSString *)path data:(NSString *)data encoding:(NSString *)encoding callback:(RCTResponseSenderBlock)callback) {
93
 RCT_EXPORT_METHOD(createFile:(NSString *)path data:(NSString *)data encoding:(NSString *)encoding callback:(RCTResponseSenderBlock)callback) {
175
 
167
 
176
 RCT_EXPORT_METHOD(writeArrayChunk:(NSString *)streamId withArray:(NSArray *)dataArray callback:(RCTResponseSenderBlock) callback) {
168
 RCT_EXPORT_METHOD(writeArrayChunk:(NSString *)streamId withArray:(NSArray *)dataArray callback:(RCTResponseSenderBlock) callback) {
177
     RNFetchBlobFS *fs = [[RNFetchBlobFS getFileStreams] valueForKey:streamId];
169
     RNFetchBlobFS *fs = [[RNFetchBlobFS getFileStreams] valueForKey:streamId];
178
-//    char bytes[[dataArray count]];
179
     char * bytes = (char *) malloc([dataArray count]);
170
     char * bytes = (char *) malloc([dataArray count]);
180
     for(int i = 0; i < dataArray.count; i++) {
171
     for(int i = 0; i < dataArray.count; i++) {
181
         bytes[i] = [[dataArray objectAtIndex:i] charValue];
172
         bytes[i] = [[dataArray objectAtIndex:i] charValue];

+ 19
- 10
src/ios/RNFetchBlobNetwork.m View File

38
 @implementation RNFetchBlobNetwork
38
 @implementation RNFetchBlobNetwork
39
 
39
 
40
 NSOperationQueue *taskQueue;
40
 NSOperationQueue *taskQueue;
41
-
42
 @synthesize taskId;
41
 @synthesize taskId;
43
 @synthesize expectedBytes;
42
 @synthesize expectedBytes;
44
 @synthesize receivedBytes;
43
 @synthesize receivedBytes;
56
     self = [super init];
55
     self = [super init];
57
     if(taskQueue == nil) {
56
     if(taskQueue == nil) {
58
         taskQueue = [[NSOperationQueue alloc] init];
57
         taskQueue = [[NSOperationQueue alloc] init];
58
+        taskQueue.maxConcurrentOperationCount = 10;
59
     }
59
     }
60
     if(taskTable == nil) {
60
     if(taskTable == nil) {
61
         taskTable = [[NSMutableDictionary alloc] init];
61
         taskTable = [[NSMutableDictionary alloc] init];
113
     // the session trust any SSL certification
113
     // the session trust any SSL certification
114
 
114
 
115
     NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
115
     NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
116
-    session = [NSURLSession sessionWithConfiguration:defaultConfigObject delegate:self delegateQueue:[NSOperationQueue mainQueue]];
116
+    session = [NSURLSession sessionWithConfiguration:defaultConfigObject delegate:self delegateQueue:taskQueue];
117
 
117
 
118
     if(path != nil || [self.options valueForKey:CONFIG_USE_TEMP]!= nil)
118
     if(path != nil || [self.options valueForKey:CONFIG_USE_TEMP]!= nil)
119
     {
119
     {
209
 
209
 
210
     if(respFile == YES)
210
     if(respFile == YES)
211
     {
211
     {
212
-        NSFileManager * fm = [NSFileManager defaultManager];
213
-        NSString * folder = [destPath stringByDeletingLastPathComponent];
214
-        if(![fm fileExistsAtPath:folder]) {
215
-            [fm createDirectoryAtPath:folder withIntermediateDirectories:YES attributes:NULL error:nil];
212
+        @try{
213
+            NSFileManager * fm = [NSFileManager defaultManager];
214
+            NSString * folder = [destPath stringByDeletingLastPathComponent];
215
+            if(![fm fileExistsAtPath:folder]) {
216
+                [fm createDirectoryAtPath:folder withIntermediateDirectories:YES attributes:NULL error:nil];
217
+            }
218
+            [fm createFileAtPath:destPath contents:[[NSData alloc] init] attributes:nil];
219
+            writeStream = [[NSOutputStream alloc] initToFileAtPath:destPath append:YES];
220
+            [writeStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
221
+            [writeStream open];
222
+        }
223
+        @catch(NSException * ex)
224
+        {
225
+            NSLog(@"write file error");
216
         }
226
         }
217
-        [fm createFileAtPath:destPath contents:[[NSData alloc] init] attributes:nil];
218
-        writeStream = [[NSOutputStream alloc] initToFileAtPath:destPath append:YES];
219
-        [writeStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
220
-        [writeStream open];
221
     }
227
     }
222
     completionHandler(NSURLSessionResponseAllow);
228
     completionHandler(NSURLSessionResponseAllow);
223
 }
229
 }
255
     
261
     
256
     if(respFile == YES)
262
     if(respFile == YES)
257
     {
263
     {
264
+        if(error != nil) {
265
+            NSLog([error localizedDescription]);
266
+        }
258
         [writeStream close];
267
         [writeStream close];
259
         callback(@[error == nil ? [NSNull null] : [error localizedDescription],
268
         callback(@[error == nil ? [NSNull null] : [error localizedDescription],
260
                    respInfo == nil ? [NSNull null] : respInfo,
269
                    respInfo == nil ? [NSNull null] : respInfo,