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