gpt4 book ai didi

ios - 返回主线程时异步 UIView 内容不显示

转载 作者:行者123 更新时间:2023-11-29 10:50:49 26 4
gpt4 key购买 nike

加载我的 UIViewController 时,我基本上是在页面中间放置一个微调器,直到内容加载完毕,然后返回主线程添加 subview :

- (void)viewDidLoad {

[super viewDidLoad];

UIActivityIndicatorView *aiv_loading = ... // etc

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

// Load content
NSString *s_checkout = [[BRNetwork sharedNetwork] getCheckoutInstructionsForLocation:self.locBooking.location];
UIView *v_invoice_content = [[BRNetwork sharedNetwork] invoiceViewForBooking:locBooking.objectId];

// Display the content
dispatch_sync(dispatch_get_main_queue(), ^{

if (s_checkout && v_invoice_content) {
[aiv_loading removeFromSuperview];
[self showContentWithText:s_checkout AndInvoice:v_invoice_content];
} else {
NSLog(@"No data received!"); // is thankfully not called
}
});
});
}

- (void) showContentWithText:(NSString *)s_checkout AndInvoice:(UIView *)v_invoice {

[self.view addSubview:[self checkoutTextWithText:s_checkout]]; // Most of the time displayed text
[self.view addSubview:[self completeCheckout]]; // always Displayed UIButton
[self.view addSubview:[self divider]]; // always displayed UIImageView

// Summary title
UILabel *l_summary = [[UILabel alloc] initWithFrame:CGRectMake(0, [self divider].frame.origin.y + 6 + 10, self.view.bounds.size.width, 20)];
l_summary.text = NSLocalizedString(@"Summary", nil);
[self.view addSubview:l_summary];

CGRect totalRect = CGRectMake([self divider].frame.origin.x, [self divider].frame.origin.y + 6 + 30, self.view.bounds.size.width - [self divider].frame.origin.x, 90);
_v_invoice = v_invoice;
_v_invoice.frame = totalRect;
[self.view addSubview:[self v_invoiceWithData:v_invoice]]; // THIS Pretty much never displayed

UITextView *l_invoice = [[UITextView alloc] initWithFrame:CGRectMake(0, _v_invoice.frame.origin.y + _v_invoice.frame.size.height + offset, 320.0, 50)];
l_invoice.text = NSLocalizedString(@"summary_emailed", nil);
[self.view addSubview:l_invoice]; // Always displayed
}

但是,并不是所有的内容都显示出来。发票一开始是不存在的,但会在几分钟后显示出来。另一个异步创建的字符串 s_content 有时不显示。

这似乎是随机的内容创建。最终结果非常简洁,但对于生产版本而言并不可靠。

我使用未记录的 [self.view recursiveDescription] 来检查是否所有内容都在那里,即使我没有看到,它们都在那里,并且似乎是正确的框架。

有什么指点吗?- layoutSubviews 没有帮助!- 将背景颜色添加到发票 View 显示背景颜色

最佳答案

我怀疑这一行是你的问题:

UIView *v_invoice_content = [[BRNetwork sharedNetwork] invoiceViewForBooking:locBooking.objectId];

正如您在后台调度队列中调用它一样。任何涉及 UIKit 的工作都应该在主队列/线程上完成。要么将其移动到主线程 block 中,要么如果构建 View 依赖于来自网络调用的数据,请更改您的 invoiceViewForBooking 方法以首先返回数据,然后在主线程中构建您的 View 数据。

   dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

// Load content
NSString *s_checkout = [[BRNetwork sharedNetwork] getCheckoutInstructionsForLocation:self.locBooking.location];
id someData = [[BRNetwork sharedNetwork] invoiceDataForBooking:locBooking.objectId];

// Display the content
dispatch_async(dispatch_get_main_queue(), ^{
UIView *v_invoice_content = [invoiceViewWithData:someData];
});
});

我还建议在主队列上使用 dispatch_async 而不是 dispatch_sync

关于ios - 返回主线程时异步 UIView 内容不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20552056/

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