0

I developed an iOS app that has two UIButton however the buttons seem to be lagging sometimes when I press them. Sometimes the lag is really bad (takes 10 seconds sometimes). I am almost positive it has something to do with the NSTimer I am using. I just want to make it so the UIViewControllers are switched as soon as the buttons are pressed, I don't want there to be any delay at all. Here is my code:

RealTimeModeViewController.m

#import "RealTimeModeViewController.h"

@interface RealTimeModeViewController ()

@end

@implementation RealTimeModeViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    [NSTimer scheduledTimerWithTimeInterval:1.0
                                     target:self
                                   selector:@selector(UpdateTime:)
                                   userInfo:nil
                                    repeats:YES];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (void)UpdateTime:(id)sender
{
    // This is where I do everything in my app
}

@end

ViewController.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    [NSTimer scheduledTimerWithTimeInterval:1.0
                                     target:self
                                   selector:@selector(UpdateTime:)
                                   userInfo:nil
                                    repeats:YES];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (void)UpdateTime:(id)sender
{
    // Most of code goes here
}

@end

The two UIButton are connected to each other ViewController via Modal style. Maybe I am supposed to put the buttons in the main thread? I am not very familiar with threads. Thank you.

user3614030
  • 481
  • 6
  • 16
  • what do you do in - (void)UpdateTime:(id)sender? can you show the code that switches between view controllers? – almas Aug 09 '14 at 23:26

2 Answers2

0

If I had to guess, you are doing something in the UpdateTime: method that is time consuming. Because it is on the main thread and it is running every 1 second, you are probably slowing down everything else. UIButton events are on the main thread, so it's likely that because you are doing everything on the main thread, it is "clogged".

For a timer implementation that won't be as accurate, but will not hang the main thread, see this answer: https://stackoverflow.com/a/8304825/3708242

If you must have the consistency of the NSTimer implementation, try reducing the amount of stuff that UpdateTime: does.

Community
  • 1
  • 1
wottle
  • 13,095
  • 4
  • 27
  • 68
0

I've had this problem before. You should use UIActivityIndicatorView

The button delay is probably caused by the UpdateTime method. Use UIActivityIndicatorView in ViewWillAppear of ViewDidLoad then perform the UpdateTime method.

Here is what I mean:

-(void) ViewDidLoad
{
UIActivityIndicatorView *act = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];

    [act setFrame:CGRectMake(320/2-50, 130, 100, 100)];
    act.layer.cornerRadius = 10;
    [act.layer setBackgroundColor:[[UIColor colorWithWhite: 0.0 alpha:0.30] CGColor]];

    UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(0, 80, act.frame.size.width, 20)];
    [lable setText:[NSString stringWithFormat:@"%@", string]];
    [lable setFont:[UIFont fontWithName:@"Helvetica-Light" size:12.0f]];
    [lable setTextAlignment:NSTextAlignmentCenter];
    [lable setTextColor:[UIColor whiteColor]];
    [act addSubview:lable];
    [self.view addSubview: act];
    [act startAnimating];

    // perform the method here, and set your delay time..
    [self performSelector:@selector(UpdateTime:) withObject:nil afterDelay:1.0];

}

Then...

- (void)UpdateTime:(id)sender
{
    // Most of your code
}

Comment and tell me if this helps :)

Ty Lertwichaiworawit
  • 2,950
  • 2
  • 23
  • 42