Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 06-29-2010, 10:51 AM   #1 (permalink)
Registered Member
 
Join Date: Dec 2009
Location: Benicia, CA
Age: 50
Posts: 152
That Don Guy is on a distinguished road
Default Handling a button action while a timer loop is running

I have an app that checks integers one at a time until it finds one that meets a particular conditions. Since this check may take quite a bit of time, I want to be able to interrupt it with a UIButton. The method I am currently using is to create a non-repeating NSTimer with an interval of 0.001 seconds that calls an action that checks the next 100 integers, then, if none of them met the conditions, create a new NSTimer identical to the previous one and add it to currentRunLoop.
There is also a boolean variable runProcess, initially set to YES, and a UIButton labelled "Stop" that sets it to NO. In the method called when the timer is fired, if runProcess = NO, the new timer is not created. (Using an NSTimer allows for the button click to be detected in the middle of the processing.)

The NSTimer is not repeating as the processing within the called method takes longer than 1 millisecond, and I do not want messages from the timer to "stack up" so they would continue to be handled even after the processing is done and the timer no longer needs to be fired.

Is this the best way to do something like this? Would having a repeating timer (and then check to see if runProcess = YES before doing any of the processing) be better? Is using a short-period NSTimer even the way to go about this?

-- Don
That Don Guy is offline   Reply With Quote
Old 06-29-2010, 10:55 AM   #2 (permalink)
Registered Member
 
Ice_2k's Avatar
 
Join Date: Apr 2010
Location: Bucharest, Romania
Posts: 148
Ice_2k is on a distinguished road
Default

I would create a background thread to handle the processing instead of this timer algorithm. The stopping method can be the same as the one you're using right now (the background thread would process 100 units, then check its runProcess flag and decide if to quit or carry on).
Ice_2k is offline   Reply With Quote
Old 06-30-2010, 08:46 AM   #3 (permalink)
Registered Member
 
Join Date: Dec 2009
Location: Benicia, CA
Age: 50
Posts: 152
That Don Guy is on a distinguished road
Default

Thanks for the tip; that works - and I was surprised that it works the way I wanted it to work without having to do anything with Run Loops. (I think the only real hoop I had to jump through was having to call performSelectorOnMainThread to get a string to display from the background thread to the main thread.

-- Don
That Don Guy is offline   Reply With Quote
Reply

Bookmarks

Tags
timer

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 327
7 members and 320 guests
anothermine, Chickenrig, Domele, givensur, michaelhansen, PixelInteractive, Sloshmonster
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,657
Threads: 94,118
Posts: 402,892
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jenniead38
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 12:23 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0