Unhiding a view is very slow in CTCallCenter callE

2019-01-23 08:14发布

Reposting with more concise and focused question after original question went unanswered. Also adding more insight into the problem after another day of research:

In my app delegate (didFinishLaunching), I set up a callEventHandler on CTCallCenter. The idea is that when a callState changes, I post a notification with a userInfo dict containing the call.callState. In my view, I observe this notification, and when the userInfo dict contains a value of CTCallDisconnected, I want to unhide a view.

The problem I'm having is that the unhiding aspect is taking, almost consistenly, ~ 7 seconds. Everything else is working fine, and I know this because I NSLog before and after the unhiding, and those logs appear immediately, but the darned view still lags for 7 seconds.

Here's my code:

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

I then listen for this notification when a user taps a button that dials a phone number:

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

Then, in 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");
   }
}

I've tracked the problem down to the if condition in the above code sample:

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

If I simply replace that with:

if (1 == 1) {

Then the view appears immediately!

The thing is, those NSLog statements are logging immediately, but the view is lagging in it's unhiding. How could that condition cause only part of it's block to execute immediately, and the rest to wait ~ 7 seconds?

Thanks!

4条回答
爷的心禁止访问
2楼-- · 2019-01-23 08:53

Hm... try to call [yourViewController.view setNeedsDisplay] after you change hidden property. Or avoid hidden, use alpha or addSubview: and removeFromSuperview methods instead.

查看更多
姐就是有狂的资本
3楼-- · 2019-01-23 09:01

Looks like there is no problem with your hidden code. If I were you, I would comment out all the code after the call ends, and uncomment them one by one to see what is the problem.

查看更多
我只想做你的唯一
4楼-- · 2019-01-23 09:07

djibouti33,

Where you put this sentence to listen when a user taps a button that dials a phone number?on WillResignActive function?

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

Thanks for your time,

Willy.

查看更多
ゆ 、 Hurt°
5楼-- · 2019-01-23 09:15

Try changing your code to this:

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

Note:

  • The parameter is an NSNotification, not an NSDictionary
  • I would not compare strings with ==
  • No need to cast the view to change the hidden property
  • Use NO instead of false

Update: Got an idea: Could you try the following, please, in between the NSLogs?

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

Reading the CTCallCenter doc, it seems the callEventHandler is dispatched on "the default priority global dispatch queue", which is not the main queue where all the UI stuff happens.

查看更多
登录 后发表回答