【问题标题】:iOS - prepareForSegue doesn't wait for the completionBlock to be completediOS - prepareForSegue 不等待完成块完成
【发布时间】:2013-01-08 12:24:19
【问题描述】:

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

}

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];
}

【问题讨论】:

  • 请展示fetchArticlesWithCompletion 的实现。它现在是异步的,所以你必须让它同步

标签: iphone ios storyboard objective-c-blocks uistoryboardsegue


【解决方案1】:

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

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

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

【讨论】:

  • 我不认为你想在任何时候在主线程上做同步的事情。最好使用回调并对此做出响应。
【解决方案2】:

我仍然更推荐 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;
}

【讨论】:

    【解决方案3】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-06
      • 1970-01-01
      • 2021-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多