Bladeren bron

#3 correct ios upload-from-storage code

Ben Hsieh 9 jaren geleden
bovenliggende
commit
7f98cb83c4
1 gewijzigde bestanden met toevoegingen van 10 en 6 verwijderingen
  1. 10
    6
      src/ios/RNFetchBlob/RNFetchBlob.m

+ 10
- 6
src/ios/RNFetchBlob/RNFetchBlob.m Bestand weergeven

440
 @synthesize filePathPrefix;
440
 @synthesize filePathPrefix;
441
 @synthesize bridge = _bridge;
441
 @synthesize bridge = _bridge;
442
 
442
 
443
+- (dispatch_queue_t) methodQueue {
444
+    return dispatch_queue_create("RNFetchBlob.queue", DISPATCH_QUEUE_SERIAL);
445
+}
446
+
443
 RCT_EXPORT_MODULE();
447
 RCT_EXPORT_MODULE();
444
 
448
 
445
 - (id) init {
449
 - (id) init {
489
                 }
493
                 }
490
                 // field contains a file
494
                 // field contains a file
491
                 else {
495
                 else {
492
-                    NSMutableData * blobData = [NSMutableData alloc];
496
+                    NSMutableData * blobData;
493
                     if(content != nil) {
497
                     if(content != nil) {
494
                         if([content hasPrefix:self.filePathPrefix]) {
498
                         if([content hasPrefix:self.filePathPrefix]) {
495
                             NSString * orgPath = [content substringFromIndex:[self.filePathPrefix length]];
499
                             NSString * orgPath = [content substringFromIndex:[self.filePathPrefix length]];
496
-                            [blobData initWithContentsOfFile:orgPath];
500
+                            blobData = [[NSData alloc] initWithContentsOfFile:orgPath];
497
                         }
501
                         }
498
                         else
502
                         else
499
-                            [blobData initWithBase64EncodedString:content options:0];
503
+                            blobData = [[NSData alloc] initWithBase64EncodedString:content options:0];
500
                     }
504
                     }
501
                     NSString * filename = [field valueForKey:@"filename"];
505
                     NSString * filename = [field valueForKey:@"filename"];
502
                     [postData appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
506
                     [postData appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
543
                                                  URLWithString: url]];
547
                                                  URLWithString: url]];
544
     
548
     
545
     NSMutableDictionary *mheaders = [[NSMutableDictionary alloc] initWithDictionary:[FetchBlobUtils normalizeHeaders:headers]];
549
     NSMutableDictionary *mheaders = [[NSMutableDictionary alloc] initWithDictionary:[FetchBlobUtils normalizeHeaders:headers]];
550
+    // move heavy task to another thread
546
     dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
551
     dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
547
         // if method is POST or PUT, convert data string format
552
         // if method is POST or PUT, convert data string format
548
         if([[method lowercaseString] isEqualToString:@"post"] || [[method lowercaseString] isEqualToString:@"put"]) {
553
         if([[method lowercaseString] isEqualToString:@"post"] || [[method lowercaseString] isEqualToString:@"put"]) {
549
             // generate octet-stream body
554
             // generate octet-stream body
550
             if(body != nil) {
555
             if(body != nil) {
551
-                NSMutableData * blobData = [NSData alloc];
552
-                // move heavy task to another thread
556
+                NSMutableData * blobData;
553
                 
557
                 
554
                 // when body is a string contains file path prefix, try load file from the path
558
                 // when body is a string contains file path prefix, try load file from the path
555
                 if([body hasPrefix:self.filePathPrefix]) {
559
                 if([body hasPrefix:self.filePathPrefix]) {
556
                     NSString * orgPath = [body substringFromIndex:[self.filePathPrefix length]];
560
                     NSString * orgPath = [body substringFromIndex:[self.filePathPrefix length]];
557
-                    [blobData initWithContentsOfFile: orgPath];
561
+                    blobData = [[NSData alloc] initWithContentsOfFile:orgPath];
558
                 }
562
                 }
559
                 // otherwise convert it as BASE64 data string
563
                 // otherwise convert it as BASE64 data string
560
                 else
564
                 else