Question

I have a strange issue.

I am running the following code and loginButton is a IBOutlet UIButton in a simple UIViewController

-(void)updateLayout{

   NSDate *startColor = [NSDate date];
   loginButton.backgroundColor = UIColorFromRGB([MSThemeManager getCoreColor]);
   pageControl.backgroundColor =UIColorFromRGB([MSThemeManager getCoreColor]);
   NSDate *methodFinishColor = [NSDate date];
   NSTimeInterval executionTimeColor = [methodFinishColor timeIntervalSinceDate:startColor];
   NSLog(@"Execution Time for getColorMethod: %f", executionTimeColor);
}

The execution time provided in the log is quite correct, so there is no issue with the getColorMethod, which is also used at a lot of different places without any issue :

2014-05-15 16:40:28.570 TnMC[9642:4007] Execution Time for getColorMethod: 0.000132

But concretely, it takes easily 10 to 20s for the color to update, on both the loginButton and the pageControl, and without any fade.

Note :
- other UI updates on loginButton that are not related to the color such as changing the titleLabel.text is instant within the same method
- there are no other processing at the same time in the app
- this is all on the main thread, and updateLayout is called because of a notification

   [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateLayout) name:nAppUpdateLayout object:nil];

Any ideas ?

Was it helpful?

Solution

Let me elaborate a little bit on this kind of issue that I'm sure many other developers that are learning iOS encountered or will encounter.

Basically whenever you are doing some basic UI change like background color, text color etc, or other UI changes that don't require heavy computation and the UI doesn't get updated you will have to check two things first (besides the correctness of the code that changes the UI).

  1. Is your UI freezing when you try to update/change something ?

  2. If your UI doesn't freeze, does it update at all, or is the code executed ?

If the first point is happening than obviously you are doing to much work on the main thread and you have to move the heavy work on a background thread.

If the second point is happening than there might be two causes. First your code is wrong and it's never called or the code is called on a background thread.

In order to determine if your code is executed on a background thread, simply put a breakpoint where you are updating the code and check the debug navigator to see the thread number, if it's not main thread, then make it on main thread.

The second way to find out if your code is on background thread is....surprise....an ugly crash.

Also a very offen problem of UI code executed on background is using NSNotification to update the UI. Rule of thumb: NSNotification is posted on the same thread as the caller.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top