Making stringWithContentsOfURL asynchronous - Is it safe?

后端 未结 3 2018
灰色年华
灰色年华 2020-12-18 05:17

I attempted to make -[NSString stringWithContentsOfURL:encoding:error:] asynchronous, by running it a-synchronically from a background thread:

__block NSStri         


        
3条回答
  •  失恋的感觉
    2020-12-18 06:00

    That should be safe, but why reinvent the wheel?

    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
    }];
    

提交回复
热议问题