Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS

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.