Ios 如何在objective-c中调用JSON数据异步以防止用户界面冻结

Ios 如何在objective-c中调用JSON数据异步以防止用户界面冻结,ios,objective-c,json,uitableview,asynchronous,Ios,Objective C,Json,Uitableview,Asynchronous,我编写了一些代码,从web服务获取一些数据,并将数据加载到iOS objective-c应用程序屏幕上的UITableView中。我发现当我启动应用程序时,UI会冻结,我确信这是因为调用viewDidload中的web服务JSON数据导致的。如何使流程异步?这是我的密码 // Calling the web service NSURL *liveTunesURL = [NSURL URLWithString:@"http://api.radiome.org/tunes/realliveTunes

我编写了一些代码,从web服务获取一些数据,并将数据加载到iOS objective-c应用程序屏幕上的UITableView中。我发现当我启动应用程序时,UI会冻结,我确信这是因为调用viewDidload中的web服务JSON数据导致的。如何使流程异步?这是我的密码

// Calling the web service
NSURL *liveTunesURL = [NSURL URLWithString:@"http://api.radiome.org/tunes/realliveTunes"];
NSData *jsonData = [NSData dataWithContentsOfURL:liveTunesURL];
NSError *error = nil;
NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
self.liveTunes = [NSMutableArray array];
NSArray *liveTunesArray = [dataDictionary objectForKey:@"Data"];

for (NSDictionary *liveTunesDictionary in liveTunesArray) {
    LiveTunes *liveTune = [LiveTunes liveTunesWithChannel:[liveTunesDictionary objectForKey:@"channel"]];
    liveTune.id = [liveTunesDictionary objectForKey:@"id"];
    liveTune.channel = [liveTunesDictionary objectForKey:@"channel"];
    liveTune.description = [liveTunesDictionary objectForKey:@"description"];
    liveTune.urlPrefix = [liveTunesDictionary objectForKey:@"urlPrefix"];
    liveTune.filename = [liveTunesDictionary objectForKey:@"filename"];
    liveTune.url = [liveTunesDictionary objectForKey:@"url"];
    liveTune.audio_stream = [liveTunesDictionary objectForKey:@"audio_stream"];
    [self.liveTunes addObject:liveTune];
}
这是我的tableView数据源代码的一部分

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
LiveTunes *liveTune = [self.liveTunes objectAtIndex:indexPath.row];

if ([liveTune.url isKindOfClass:[NSString class]]) {
    NSData *imageData = [NSData dataWithContentsOfURL:liveTune.thumbnailURL];
    UIImage *image = [UIImage imageWithData:imageData];

    cell.imageView.image = image;
}
else {
    cell.imageView.image = [UIImage imageNamed:@"music_cell.png"];
}

cell.textLabel.text = liveTune.channel;
cell.detailTextLabel.text = liveTune.description;

return cell;
}


有人能帮我把这个过程变成异步的,这样UI就不会冻结了。

看看如何使用NSMutableURLRequest。一旦有了请求对象,就可以这样使用它

AFHTTPRequestOperation *af = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    [af setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    //Success
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    //Failure
        UIAlertController *confirmDialog =
        [UIAlertController alertControllerWithTitle:@"Error"
                                            message:[NSString stringWithFormat:@"Request Failed - Status %ld (%@)", (long)[operation.response statusCode], error.description]
                                     preferredStyle:UIAlertControllerStyleAlert];
        [confirmDialog addAction:[UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDestructive handler:nil]];
        [self presentViewController:confirmDialog animated:true completion:nil];
    }];

    [af start];

您可以使用GCD或NSURLConnection的异步方法。但是,我建议使用
AFNetworking
。它更简单、更安全。我强烈建议您使用。如果您的目标是iOS8及以上版本,也可以使用
NSURLSession
。它不需要第三方库。