2012-07-23 27 views
6

मैं बनाने का प्रयास किया - [NSString stringWithContentsOfURL: एन्कोडिंग: त्रुटि:] अतुल्यकालिक, यह एक पृष्ठभूमि धागे से एक-synchronically चलाकर: अपनी अतुल्यकालिकस्ट्रिंग WithContentsOfURL एसिंक्रोनस बनाना - क्या यह सुरक्षित है?

__block NSString *result; 
dispatch_queue_t currentQueue = dispatch_get_current_queue(); 

void (^doneBlock)(void) = ^{ 
    printf("done! %s",[result UTF8String]); 
}; 

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 
             (unsigned long)NULL), ^(void) { 
    result = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://www.google.com/"] encoding:NSUTF8StringEncoding error:nil]; 
    dispatch_sync(currentQueue, ^{ 
     doneBlock(); 
    }); 
}); 

इसके ठीक काम कर रहा है, और सबसे महत्वपूर्ण बात,।

मेरा सवाल यह है कि यह करना सुरक्षित है, या कोई थ्रेडिंग समस्या आदि हो सकती है?

अग्रिम :)

उत्तर

27

कि सुरक्षित होना चाहिए, लेकिन क्यों पहिया बदलने धन्यवाद?

NSURLRequest *req = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com"]]; 
[NSURLConnection sendAsynchronousRequest:req queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { 
    NSString *result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; 
    // etc 
}]; 
+0

चीयर्स! मुझे नहीं पता था कि यह संभव था: पी – JonasG

+0

सबसे पहले मैं यह कहता हूं कि यह '[NSOperationQueue mainQueue]' की वजह से मुख्य कतार पर काम करेगा, लेकिन मैंने 'sendAsynchronousRequest' को देखा है। तो यह यूआई को अपडेट करने से नहीं रोकना चाहिए। –

0

तुम भी उपयोग कर सकते हैं:

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); 

dispatch_async(queue, ^{ 
     NSError *error = nil; 
     NSString *searchResultString = [NSString stringWithContentsOfURL:[NSURL URLWithString:searchURL] 
                  encoding:NSUTF8StringEncoding 
                   error:&error]; 
     if (error != nil) { 
      completionBlock(term,nil,error); 
     } 
     else 
     { 
      // Parse the JSON Response 
      NSData *jsonData = [searchResultString dataUsingEncoding:NSUTF8StringEncoding]; 
      NSDictionary *searchResultsDict = [NSJSONSerialization JSONObjectWithData:jsonData 
                       options:kNilOptions 
                       error:&error]; 
      if(error != nil) 
      { 
       completionBlock(term,nil,error); 
      } 
      else 
      { 

       //Other Work here 
      } 
     } 
    }); 

लेकिन हाँ, यह सुरक्षित होना चाहिए। मुझे बताया गया है कि त्रुटि कॉल के कारण NSRLConnection का उपयोग करने के लिए और इंटरनेट के माध्यम से संचार करते समय। मैं अभी भी इसमें शोध कर रहा हूं।

0
-(void)loadappdetails:(NSString*)appid { 
    NSString* searchurl = [@"https://itunes.apple.com/lookup?id=" stringByAppendingString:appid]; 

    [self performSelectorInBackground:@selector(asyncload:) withObject:searchurl]; 

} 
-(void)asyncload:(NSString*)searchurl { 
    NSURL* url = [NSURL URLWithString:searchurl]; 
    NSError* error = nil; 
    NSString* str = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error]; 
    if (error != nil) { 
     NSLog(@"Error: %@", error); 
    } 
    NSLog(@"str: %@", str); 
}