iphone - iOS - prepareForSegue 不等待完成 block 完成

标签 iphone ios storyboard objective-c-blocks uistoryboardsegue

我喜欢的:等到数据下载完成然后打开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];
}

最佳答案

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

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

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

关于iphone - iOS - prepareForSegue 不等待完成 block 完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14506559/

相关文章:

android - 在 Android/iOS 浏览器上呈现多个选择选项

ios - 从 ViewController( Storyboard)调用 NavigationController (xib)

ios - Storyboard:如何在更改布局约束时让 Xcode 自动更新框架?

storyboard - 我可以在 iOS 11 中通过 Storyboard 添加 PDFView

iPhone OpenGL : Using gluUnProject (port) and detecting clicking on an object

iphone - 我想在 NSObject 类中添加 MBProgressHUD

iphone - 如何从 CGImage 获取原始像素数据

iphone - 在 UIWebView 中点击以隐藏键盘

iphone - UITableViewCell 重用 : cell displaying incorrectly - not being reused as expected

javascript - 如何在 React Native 中调用 AlertIOS