Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7590827
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T20:30:05+00:00 2026-05-30T20:30:05+00:00

I’m looking at Sam’s Teach Yourself iPhone dev and I don’t understand the example

  • 0

I’m looking at Sam’s Teach Yourself iPhone dev and I don’t understand the example given for backgrounding. The non backgrounding code is:

  - (void)viewDidLoad {
       [super viewDidLoad];
       count=0;
       theTimer=[NSTimer scheduledTimerWithTimeInterval:0.1
                       target:self
                       selector:@selector(countUp)
                       userInfo:nil
                       repeats:YES];
   }

And the backgrounding version is:

- (void)viewDidLoad {
    [super viewDidLoad];

    counterTask = [[UIApplication sharedApplication]
              beginBackgroundTaskWithExpirationHandler:^{
                 // If you're worried about exceeding 10 minutes, handle it here
              }];
    count=0;
    theTimer=[NSTimer scheduledTimerWithTimeInterval:0.1
                    target:self
                    selector:@selector(countUp)
                    userInfo:nil
                    repeats:YES];
}

What I don’t understand is where the associated is between the activity to be performed in the background and beginBackgroundTaskWithExpirationHandler.

In this example the the NSTimer is running in the background – but what if there was some other activity to be performed in the background as well i.e. suppose the code is:

- (void)viewDidLoad {
    [super viewDidLoad];

    counterTask = [[UIApplication sharedApplication]
              beginBackgroundTaskWithExpirationHandler:^{
                 // If you're worried about exceeding 10 minutes, handle it here
              }];
    count=0;
    theTimer=[NSTimer scheduledTimerWithTimeInterval:0.1
                    target:self
                    selector:@selector(countUp)
                    userInfo:nil
                    repeats:YES];

  x: some other activity to be performed in the background
  maybe another timer with a difference time interval
}

How could you additionally specify that x also gets executed in the background?

Or have I misunderstood how it works and when beginBackgrounTaskWithExpirationHandler is called it is the the whole of the application whcih will in fact execute in the background? If that is the case then why is a task identifier necessary as you would only be able to start one task which is your app?

If that’s not the case and it is possible to pick and choose different tasks to execute in the background, then how is this implemented? Suppose in this example X is a second timer with a different interval and a different expiration condition, what would the code look like if I wanted both theTimer and x to execute in the background? In other words what if the code was this:

- (void)viewDidLoad {
    [super viewDidLoad];

    counterTask = [[UIApplication sharedApplication]
              beginBackgroundTaskWithExpirationHandler:^{
                 // If you're worried about exceeding 10 minutes, handle it here
              }];
    count=0;
    theTimer=[NSTimer scheduledTimerWithTimeInterval:0.1
                    target:self
                    selector:@selector(countUp)
                    userInfo:nil
                    repeats:YES];

    theTimer2=[NSTimer scheduledTimerWithTimeInterval:1.0
                    target:self
                    selector:@selector(aDifferentMethod)
                    userInfo:nil
                    repeats:YES];

    theTimer3=[NSTimer scheduledTimerWithTimeInterval:10.0
                    target:self
                    selector:@selector(anotherDifferentMethod)
                    userInfo:nil
                    repeats:YES];

}

How to specify that both theTimer and theTimer2 execute in the background but theTimer3 doesn’t?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T20:30:07+00:00Added an answer on May 30, 2026 at 8:30 pm

    Or have I misunderstood how it works and when beginBackgrounTaskWithExpirationHandler is called it is the the whole of the application whcih will in fact execute in the background? If that is the case then why is a task identifier necessary as you would only be able to start one task which is your app?

    The whole application will run. It’s up to you to determine what to do or not do. The point of beginBackgroundTaskWithExpirationHandler: is that it tells the system that you have some long-running thing to do. It makes a note, and hands you back a token. When your long running task is finished, you call endBackgroundTask: with that token. It’s possible that during that time, you never actually went into the background.

    When your application is going to be suspended, the system looks to see if you have any pending tokens. If you don’t, then it just suspends you. If you do, then it lets you run for a while more. The system doesn’t care how you associate those tokens with work. You could just use a single token for your entire program. Or each object could keep its own token when it needed to do something. The point is that as long as you have one token, you’re generally going to be given some extra time to work before being suspended.

    To your question about timers, you generally shouldn’t do any timer work in the background. You don’t have a lot of time (10 minutes at most). You need to do your work and get out. You should be suspending (invalidating) all your timers when you go in the background. You then set them back up when you resume.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
i got an object with contents of html markup in it, for example: string
I have this code to decode numeric html entities to the UTF8 equivalent character.
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I would like to count the length of a string with PHP. The string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.