1

我喜欢的:等到数据下载完成然后打开TableView并显示data

我所拥有的:什么时候prepareForSegue被称为TableView立即打开而不等待data下载,尽管我有一个completionBlock(我猜可能没有正确实现)。

注意:当我返回并TableView再次打开时,我看到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;
    }

}

商店.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];
}
4

3 回答 3

2

您应该在执行序列之前加载数据。

- (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
}

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

于 2013-01-24T16:59:46.280 回答
1

我仍然更推荐 Joris 的回答,但理论上,你可以做一些时髦的事情,比如:

- (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender
{
    if ([identifier isEqualToString:@"segueIdentifier"] && !_didFinishExecutingBlock)
    {
        [self methodWithCompletionBlock:^{
            _didFinishExecutingBlock = YES;
            [self.navigationController performSegueWithIdentifier:identifier sender:self];
        }];
        return false;
    }
    else
        return true;
}
于 2014-11-21T21:53:01.907 回答
0

它不会等待,因为您使用的是块,并且在声明完成后立即执行,解决方案是删除块

- (void)fetchEntries
{
        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;
    }

}
于 2013-01-24T16:57:03.583 回答