gpt4 book ai didi

ios - 在 UITableView Segues 之前运行后台线程

转载 作者:行者123 更新时间:2023-11-29 00:55:09 25 4
gpt4 key购买 nike

我有一个显示电影列表的 UITableView。 (电影名称和 ID 存储在预先填充的数组中)。

当我单击一行时,我希望应用程序在继续之前进入后台,下载“电影信息”,然后在后台线程完成后继续到新 View 。信息的下载工作正常,所以我省略了那部分代码。

但是,关于如何显示微调器以让用户知道正在发生的事情然后在后台完成时转换或失败,我似乎无法理解我的逻辑。

有什么帮助吗?

在我的 UITableViewController 类中

    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
MovieDetailsViewController *mvc = (MovieDetailsViewController*)segue.destinationViewController;
mvc.movie = movie;
}

- (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender {
//I KNOW THAT IF THIS RETURNS TRUE prepareForSegue is called
bool canSegue = NO:
if ([identifier containsString:@"seg_movie"]) {

// SHOW Activity spinner

[self doMovieLookup];

if (movie != nil) {
canSegue = YES;
}
}
return canSegue;

}



- (void) doMovieLookup {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//HERE I AM COMMUNCATING WITH THE SERVER AND PARSING JSON RESULTS INTO A DICTIONARY
//this code works fine then I send the JSON results on to object creation.
[self buildMovieWithDictionary:[[dictionaryResults objectForKey:@"Movie"] objectAtIndex:0]];

dispatch_async(dispatch_get_main_queue(), ^{

//DO I NEED SOMETHING HERE?

//STOP SPINNER

});

});

}

只是一个创建 Movie 对象的方法,以防有人好奇。

- (void) buildMovieWithDictionary : (NSDictionary*) dictionary {
movie = [[Movie alloc] init];
movie.title = [dictionary objectForKey:@"MovieTitle"];
movie.description = [dictionary objectForKey:@"MovieTitle"];
movie.releasedate = [dictionary objectForKey:@"MovieTitle"];
}

最佳答案

你很接近,但我觉得有点乱。不要在 shouldPerformSegueWithIdentifier:sender: 中调用 doMovieLookup,而是尝试在 tableView:didSelectRowAtIndexPath 中执行此操作。

然后,您可以调用 performSegueWithIdentifier:sender:,让您的 dispatch_async 回调到主线程。

关于ios - 在 UITableView Segues 之前运行后台线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37666418/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com