iOS-prepareForSegue不等待完成区块完成 [英] iOS - prepareForSegue doesn't wait for the completionBlock to be completed

查看:94
本文介绍了iOS-prepareForSegue不等待完成区块完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我喜欢的东西:等待数据下载完成,然后打开TableView并显示data.

What I like: Wait until data download completes then open TableView and show data.

我所拥有的:尽管我有一个completionBlock,但调用prepareForSegue时,TableView立即打开,而无需等待data下载(我猜这可能无法正确实现) ).

What I have: When prepareForSegue is called the TableView opens immediately without waiting for the data download although I have a completionBlock (which may not be implemented correctly I guess).

注意:当我返回并再次打开TableView时,会看到data.

Note: When I go back and open the TableView again I see the data.

- (void)fetchEntries
{
    void (^completionBlock) (NSArray *array, NSError *err) = ^(NSArray *array, NSError *err)
    {
        if (!err)
        {
            self.articlesArray = [NSArray array];
            self.articlesArray = array;
        }
    };

    [[Store sharedStore] fetchArticlesWithCompletion:completionBlock];
}


-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    [self fetchEntries];

    if ([[segue identifier] isEqualToString:@"ShowArticles"])
    {
        TableVC *tbc = segue.destinationViewController;
        tbc.articlesArrayInTableVC = self.articlesArray;
    }

}

Store.m

- (void)fetchArticlesWithCompletion:(void (^) (NSArray *channelObjectFromStore, NSError *errFromStore))blockFromStore
{
    NSString *requestString = [API getLatestArticles];

    NSURL *url = [NSURL URLWithString:requestString];

    NSURLRequest *req = [NSURLRequest requestWithURL:url];

    Connection *connection = [[Connection alloc] initWithRequest:req];

    [connection setCompletionBlockInConnection:blockFromStore];

    [connection start];
}

推荐答案

在执行定序操作之前,应先加载数据.

You should load your data before you perform a seque.

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // show loading indicator
    __weak typeof(self) weakSelf = self;
    [[Store sharedStore] fetchArticlesWithCompletion:^(NSArray *array, NSError *err)
    {
        [weakSelf performSegueWithIdentifier:@"ShowArticles" sender:weakSelf];
        // hide loading indicator
    }];
}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // do whatever
}

尽管在我看来,响应用户交互立即显示下一个视图控制器要好得多.您是否考虑过将数据加载到下一个视图控制器中,而不是在实际转换之前等待它?

Although in my opinion it is much nicer to immediately show the next view controller in response to a user interaction. Have you considered loading your data in the next view controller, instead of waiting for it before you actually want to transition?

这篇关于iOS-prepareForSegue不等待完成区块完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆