Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial


Lee Watkins
Lee Watkins
11,345 Points

Calling a function in the "distant future"

Hi all!

I'm trying to find a way to get my code to execute a function in the "distant future" and looking for some help.

By distant future, I mean longer than an app may live in the background (3 minutes in iOS 7.1.1 I think.) So, say in 1 hour's time.

Is there a way that I can tell my app to wake up and do something in 1 hour from now?

I know of:

  • performSelector:withObject:afterDelay:
  • dispatch_after, and
  • scheduling local notifications

but will any of these work, and how would one implement them? And what happens to said action if my app crashes in the background for some reason?

I think local notification would be great (because I would like to notify the user in some cases,) but the problem comes in (I think) if the user doesn't launch the app when the notification is presented.

Thanks UniekLee

2 Answers

Rodrigo Chousal
Rodrigo Chousal
16,009 Points

Have you considered sleep()? You write the number of seconds you want the program to stop running in between the parenthesis.

Lee Watkins
Lee Watkins
11,345 Points

Hi Rodrigo Chousal

Thanks for the suggestion. I've used sleep() in other places, but sleep() pauses the execution of your app/thread. I've had issues in the past using sleep(), so I'm trying to steer clear of it.