Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Dec 10, 2008, 02:32 PM   #1
JoshAK
macrumors newbie
 
Join Date: Aug 2008
sleep() question

I'm trying to write a script that will update an image then pause or sleep for 2 seconds and then set a different image.

Currently I'm using sleep(2). But it sleeps first and then updates the images after. I want to update first then pause then update again. Any help would be nice. Thanks
JoshAK is offline   0 Reply With Quote
Old Dec 10, 2008, 03:53 PM   #2
firewood
macrumors 603
 
Join Date: Jul 2003
Location: Silicon Valley
Any sleep() or delay inside the main thread blocks all UI activity, including pending display updates. Thus, it's the wrong paradigm for Cocoa. Don't do it.

Instead, learn to use NSTimers to call your methods, and let these methods do what you want to do at any point in time in the future. You may have to save the state of what you want to do next or later in static, global, or object variables, but that's a trivial problem for the student.

The only time this may be difficult is inside deeply recursive routines. You may want to run those in a background (non UI) thread instead.

.
firewood is offline   0 Reply With Quote
Old Dec 10, 2008, 04:04 PM   #3
JoshAK
Thread Starter
macrumors newbie
 
Join Date: Aug 2008
Thanks!

Thanks for the reply. Can you give me an example of how I could us an NSTimer to delay or pause for 2 seconds? I'm new to xcode/cocoa and I'm not sure how to accomplish this. Thanks for the help.
JoshAK is offline   0 Reply With Quote
Old Dec 10, 2008, 04:39 PM   #4
firewood
macrumors 603
 
Join Date: Jul 2003
Location: Silicon Valley
Google turned up this:

http://icodeblog.com/2008/10/28/ipho...ng-an-nstimer/

and several other examples. YMMV.

There's also tons of documentation on Objective C, Cocoa and NSTimers on Apple's dev site.


.
firewood is offline   0 Reply With Quote
Old Dec 10, 2008, 08:40 PM   #5
JoshAK
Thread Starter
macrumors newbie
 
Join Date: Aug 2008
Thanks

Works great! Thanks!!
JoshAK is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

Forum Jump

Similar Threads
thread Thread Starter Forum Replies Last Post
Battery Question MilkMan4 MacBook Air 2 Jul 25, 2011 10:41 AM
Safari Question! MilkMan4 Mac Applications and Mac App Store 6 Jul 22, 2011 01:51 AM
Flashing Question Mark has me pulling my hair out momech OS X 8 Dec 30, 2010 11:47 PM
Just a Question about Next Gen Macbook Pro (not a whether to buy now or not) macnerd123 MacBook Air 7 Dec 20, 2010 09:48 PM
Flashing question mark folder on startup Penny's Geek OS X 6 Nov 17, 2010 07:38 PM


All times are GMT -5. The time now is 10:21 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC