6

-[NSString stringWithContentsOfURL:encoding:error:] を非同期にしようとしましたが、バックグラウンド スレッドから非同期で実行しました。

__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();
    });
});

正常に動作し、最も重要なのは非同期です。

私の質問は、これを行うのが安全かどうか、またはスレッドの問題などがある可能性があるかどうかです.

前もって感謝します :)

4

3 に答える 3

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
}];
于 2012-07-23T22:41:01.943 に答える
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);
}
于 2013-05-15T07:04:32.830 に答える
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
            }
        }
    });

しかし、はい、それは安全でなければなりません。ただし、インターネット経由で通信する場合のエラーコールなどのために、代わりに NSURLConnection を使用するように言われました。私はまだこれについて研究を行っています。

于 2013-04-21T15:35:01.637 に答える