gpt4 book ai didi

iphone - 在 CTCallCenter callEventHandler 中取消隐藏 View 非常慢

转载 作者:可可西里 更新时间:2023-11-01 03:29:16 25 4
gpt4 key购买 nike

在原始问题未得到解答后,重新发布更简洁、重点更突出的问题。在又一天的研究之后,还增加了对问题的更多见解:

在我的应用委托(delegate) (didFinishLaunching) 中,我在 CTCallCenter 上设置了一个 callEventHandler。这个想法是,当 callState 发生变化时,我会发布一个带有 userInfo dict 的通知包含 call.callState。在我看来,我观察到这个通知,当userInfo dict 包含一个值 CTCallDisconnected,我想取消隐藏 View 。

我遇到的问题是,取消隐藏方面几乎始终需要 ~ 7 秒。其他一切都工作正常,我知道这一点,因为我在取消隐藏之前和之后 NSLog,这些日志立即出现,但该死的 View 仍然滞后 7 秒。

这是我的代码:

appDidFinishLaunching:

self.callCenter = [[CTCallCenter alloc] init];
self.callCenter.callEventHandler = ^(CTCall* call) {
// anounce that we've had a state change in our call center
NSDictionary *dict = [NSDictionary dictionaryWithObject:call.callState forKey:@"callState"];
[[NSNotificationCenter defaultCenter] postNotificationName:@"CTCallStateDidChange" object:self userInfo:dict];
};

然后,当用户点击调用电话号码的按钮时,我会监听此通知:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ctCallStateDidChange:) name:@"CTCallStateDidChange" object:nil];

然后,在 ctCallStateDidChange 中:

- (void)ctCallStateDidChange:(NSNotification *)notification
{
NSLog(@"121");
NSString *callInfo = [[notification userInfo] objectForKey:@"callState"];
if ([callInfo isEqualToString:CTCallStateDisconnected]) {
NSLog(@"before show");
[self.view viewWithTag:kNONEMERGENCYCALLSAVEDTOLOG_TAG].hidden = NO;
NSLog(@"after show");
}
}

我已经将问题追溯到上面代码示例中的 if 条件:

 if ([[userInfo valueForKey:@"userInfo"] valueForKey:@"callState"] == CTCallStateDisconnected) {

如果我简单地将其替换为:

if (1 == 1) {

然后 View 立即出现!

问题是,那些 NSLog 语句会立即记录,但 View 是落后于它是不隐藏的。这种情况怎么可能只导致部分阻塞立即执行,其余等待 ~ 7 秒?

谢谢!

最佳答案

尝试将您的代码更改为:

- (void)ctCallStateDidChange:(NSNotification *)notification
{
NSLog(@"121");
NSString *callInfo = [[notification userInfo] objectForKey:@"callState"];
if ([callInfo isEqualToString:CTCallStateDisconnected]) {
NSLog(@"before show");
[self.view viewWithTag:kNONEMERGENCYCALLSAVEDTOLOG_TAG].hidden = NO;
NSLog(@"after show");
}
}

注意:

  • 参数是一个NSNotification,而不是一个NSDictionary
  • 我不会将字符串与 == 进行比较
  • 无需转换 View 来更改隐藏属性
  • 使用NO代替false

更新:有一个想法:请在 NSLog 之间尝试以下操作好吗?

dispatch_async(dispatch_get_main_queue(), ^{
[self.view viewWithTag:kNONEMERGENCYCALLSAVEDTOLOG_TAG].hidden = NO;
});

阅读 CTCallCenter 文档,似乎 callEventHandler 是在“默认优先级全局调度队列”上调度的,这不是所有 UI 内容发生的主队列.

关于iphone - 在 CTCallCenter callEventHandler 中取消隐藏 View 非常慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6274642/

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