gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 19:13:58 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com