Take the 2-minute tour ×

I'm developing iPhone app which runs in the background(iOS4), and refer "Completing a Finite Length Task in the Background" written by Apple at the following url


Then I've been able to implement background tasks. (Of course, I see that application has 10min time limitaiton.) However, I still can't understand what "bgTask = UIBackgroundTaskInvalid;"(Line7,16 of Listing 4-2) means.

In my opinion, the line shown above has never been reached. Because there is "endBackgroundTask:" before that and the background task will be ended. In fact, when I checked with xcode debugger, this thought may be true and not reach at Line7, 16.

If so, is this line redundant? Or is there any reason to have to be written?

I would appreciate any help about this. Thanks in advance.

share|improve this question
add comment

1 Answer

The code in the block is called if the 10 minutes runs out before the application has completed its background task.

The code in this block must call endBackground: to indicate the situation is acknowledged and accepted by the application - if it doesn't the application will be terminated. Note that calling the method doesn't terminate the application - it simply indicates to the OS that the background task execution has completed.

The second line is simply to reset bgTask to a neutral value, rather than leaving it set id of a task that no longer exists. It's a tidiness thing rather than being essential.

(I wouldn't be surprised if the second line isn't executed until the application is next foregrounded, since once background execution ends the app doesn't get any CPU time to run. Haven't tested this, though)

share|improve this answer
add comment

Your Answer


