Problem with using NSTimer in another thread

Discussion in 'iOS Programming' started by antwerp, Oct 27, 2009.

  1. antwerp macrumors newbie

    Oct 22, 2009
    Hi, guys!

    I'm developing some app where I need to make some precision operations in background thread every 1/x of second. The application have UIScrollView component and I want to not idle with this operations while UIScrollView making some scrolling (I have these idles if I try to make this operations on main thread). After trying to make what I need in my application I decided to make tiny sample to get the same behaviour there before and then implement it into my application. But I can't get it there.

    So, what have I done:

    1) I have created simple application with only one UIScrollView component and UIButton on it. Scrollview has content size 1000x1000, the button launch some background process.
    Here is what I have done in the main controller:

    - (void)applicationDidFinishLaunching:(UIApplication *)application {    
        [window makeKeyAndVisible];
        [scrollView setContentSize:CGSizeMake(1000, 1000)];
        model=[[Model alloc] init];
    - (IBAction) click:(id)sender{
         thread=[[NSThread alloc] initWithTarget:model selector:@selector(launchTimer) object:nil];
         [thread start];
    Everything looks clear I think. And this is content of the Model implementation:

    - (id) init{
         self=[super init];
         return self;
    - (void) launchTimer{
         NSAutoreleasePool *pool=[[NSAutoreleasePool alloc] init];
         NSRunLoop *runLoop=[NSRunLoop currentRunLoop];
         timer=[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(tick) userInfo:nil repeats:YES];
         [runLoop run];
         [pool release];
    - (void) tick{     
    So I have create timer which must works in another run loop, right? So it is mean that when I move my scroll view there isn't any delay with invoking tick method, right?

    To very simple check it I add breakpoint into tick method which "Sound out and auto continue" and then launch this application on my iPhone. And what I notice in it? When I click on the button I hear a chain of sounds on my Mac, everything looks good, but when I begin to scroll the scrollView this sounds begin to idle! The delay between sounds could be sometimes about 0.5-0.6 of second - it's more than in 5 times longer than I set in the timer.

    Could you tell me where is a problem?

    By the way, I'm seeing in Debugger Console the next:

    [Switching to thread 11779]
    [Switching to thread 11779]
    (gdb) continue
    2009-10-26 13:36:55.232 testthreads[530:207] click
    2009-10-26 13:36:55.270 testthreads[530:5023] launchTimer
    [Switching to thread 12803]
    [Switching to thread 12803]
    2009-10-26 13:36:55.839 testthreads[530:5023] tick
    2009-10-26 13:36:55.940 testthreads[530:5023] tick
    2009-10-26 13:36:56.051 testthreads[530:5023] tick

    I don't know and can't find anywhere what does it mean [530:5023] and [530:207]. Are numbers 207 and 5023 mean different threads? If it is so, then my tick method invokes in another thread with scrollview controller but why I get idle there?


Share This Page