views:

70

answers:

1

Hi all, Ive been trying to get my game to work correctly with an NSTimer. Ive seen many people have had a similar problem to me and I just need a bit of clarification on something.

Basically I have an NSTimer running on the main thread which is updating images which represent the time, but I also have a mapView. When the user pans the map the timer is blocked. My question is if I create a new thread and add the timer to its runloop, when I perform selector (which updates UI) will this not block the timer thread again? Also I know it is bad practise to update the UI from secondary thread so how would I go about that?

Any help would be much appreciated

Many thanks

Jules

Hi thanks again for your help. I think the mapView was blocking the timer as they were both running in the same run loop. I have now fixed this with a timer thread with its own run loop, however this has led me to a 2nd problem which has me extremely stuck!! Here is the code...

//called when I need to restart the timer [NSThread detachNewThreadSelector:@selector(resumeTimer) toTarget:self withObject:nil];

-(void) restartTimer {

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
timer=[NSTimerscheduledTimerWithTimeInterval:1.              target:self                            selector:@selector(dim) userInfo:nil repeats:YES];

[self performSelectorOnMainThread:@selector(timerImageUpdate) withObject:nil waitUntilDone:NO];

[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
[[NSRunLoop currentRunLoop] run];

[pool drain];

}

This code gives me a Bad_access error on the [pool drain]; I have run the code in instruments and still cannot see why it gives me the error. Any ideas?

Many thanks

Jules

A: 

If you create a thread for your timer, you still have to do the UI update on the main thread. You can do that with performSelectorOnMainThread:withObject:waitUntilDone:NO which will queue the method call on the main thread without blocking the timer thread.

However, if the main thread's runloop is blocked by the map panning (why?) the UI update will still be waiting in the event queue for the until the map pan is done.

progrmr
Thanks for the reply. I think the timer is blocking as it is running on the main threads run loop alsom so when the user pans the map the ui update is blocking the timer?? Is this the expected behaviour or am I just doing this completely wrong?Many thanksJules
Jules
You can create an [NSThread](http://developer.apple.com/iphone/library/documentation/Cocoa/Reference/Foundation/Classes/NSThread_Class/Reference/Reference.html) to run the timer in, but I don't think the mapview should be blocking the main thread. I've never used mapview so I don't know how it behaves, did you study [MKMapView docs](http://developer.apple.com/iphone/library/documentation/MapKit/Reference/MKMapView_Class/MKMapView/MKMapView.html)? Maybe you want to rephrase your question to be "why does my map view block the main thread?" and focus on that instead.
progrmr
I have updated my question...please have a look above and thank you for your help. (apologies for formatting of code)
Jules