Ben Hsieh преди 8 години
родител
ревизия
11a173bd21
променени са 2 файла, в които са добавени 26 реда и са изтрити 26 реда
  1. 7
    16
      src/ios/RNFetchBlob/RNFetchBlob.m
  2. 19
    10
      src/ios/RNFetchBlobNetwork.m

+ 7
- 16
src/ios/RNFetchBlob/RNFetchBlob.m Целия файл

@@ -81,21 +81,13 @@ RCT_EXPORT_METHOD(fetchBlob:(NSDictionary *)options
81 81
                   body:(NSString *)body callback:(RCTResponseSenderBlock)callback)
82 82
 {
83 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 93
 RCT_EXPORT_METHOD(createFile:(NSString *)path data:(NSString *)data encoding:(NSString *)encoding callback:(RCTResponseSenderBlock)callback) {
@@ -175,7 +167,6 @@ RCT_EXPORT_METHOD(writeStream:(NSString *)path withEncoding:(NSString *)encoding
175 167
 
176 168
 RCT_EXPORT_METHOD(writeArrayChunk:(NSString *)streamId withArray:(NSArray *)dataArray callback:(RCTResponseSenderBlock) callback) {
177 169
     RNFetchBlobFS *fs = [[RNFetchBlobFS getFileStreams] valueForKey:streamId];
178
-//    char bytes[[dataArray count]];
179 170
     char * bytes = (char *) malloc([dataArray count]);
180 171
     for(int i = 0; i < dataArray.count; i++) {
181 172
         bytes[i] = [[dataArray objectAtIndex:i] charValue];

+ 19
- 10
src/ios/RNFetchBlobNetwork.m Целия файл

@@ -38,7 +38,6 @@ NSMutableDictionary * taskTable;
38 38
 @implementation RNFetchBlobNetwork
39 39
 
40 40
 NSOperationQueue *taskQueue;
41
-
42 41
 @synthesize taskId;
43 42
 @synthesize expectedBytes;
44 43
 @synthesize receivedBytes;
@@ -56,6 +55,7 @@ NSOperationQueue *taskQueue;
56 55
     self = [super init];
57 56
     if(taskQueue == nil) {
58 57
         taskQueue = [[NSOperationQueue alloc] init];
58
+        taskQueue.maxConcurrentOperationCount = 10;
59 59
     }
60 60
     if(taskTable == nil) {
61 61
         taskTable = [[NSMutableDictionary alloc] init];
@@ -113,7 +113,7 @@ NSOperationQueue *taskQueue;
113 113
     // the session trust any SSL certification
114 114
 
115 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 118
     if(path != nil || [self.options valueForKey:CONFIG_USE_TEMP]!= nil)
119 119
     {
@@ -209,15 +209,21 @@ NSOperationQueue *taskQueue;
209 209
 
210 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 228
     completionHandler(NSURLSessionResponseAllow);
223 229
 }
@@ -255,6 +261,9 @@ NSOperationQueue *taskQueue;
255 261
     
256 262
     if(respFile == YES)
257 263
     {
264
+        if(error != nil) {
265
+            NSLog([error localizedDescription]);
266
+        }
258 267
         [writeStream close];
259 268
         callback(@[error == nil ? [NSNull null] : [error localizedDescription],
260 269
                    respInfo == nil ? [NSNull null] : respInfo,