在UITableView Segues之前运行后台线程

时间:2016-06-06 20:34:15

标签: ios objective-c multithreading uitableview

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

当我点击一行时,我希望应用程序进入后台进行预设,下载“电影信息”,然后在后台线程完成后下载到新视图。下载信息工作正常,所以我遗漏了部分代码。

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

任何帮助?

在我的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"];       
}

1 个答案:

答案 0 :(得分:1)

你关闭了,但我觉得有点乱。不要在doMovieLookup中调用shouldPerformSegueWithIdentifier:sender:,而是在tableView:didSelectRowAtIndexPath中进行操作。

然后,您可以致电performSegueWithIdentifier:sender:,将dispatch_async号召回主线。