What does "UIBackgroundTaskInvalid" mean?

I am developing an application for the iPhone that runs in the background (iOS4), and references the “Job of finite length task in the background” written by Apple at the following URL

http://developer.apple.com/library/ios/#documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html#//apple_ref/doc/uid/TP40007072-CH5

Then I managed to complete the background tasks. (Of course, I see that the application has a 10min time limitaiton.) However, I still don't understand what “ bgTask = UIBackgroundTaskInvalid; ” means (line 7.16 in Listing 4-2).

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

If so, is this line redundant? Or is there some reason to write?

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

+4
source share
2 answers

The code in the block is called if it ends 10 minutes before the application completes the background job.

The code in this block should call endBackground: to indicate that the situation is confirmed and accepted by the application - if the application is not terminated. Note that the method call does not terminate the application - it simply indicates to the OS that the background task has completed.

The second line simply equals reset bgTask to a neutral value, and does not leave the given identifier for a task that no longer exists. This thing is neat, not essential.

(I won’t be surprised if the second line is not executed until the application is in the next environment, because after the background is completed, the application does not receive any processor time to start. However, I did not check this)

+10
source

The key to understanding is that instead of a completion handler, you have an expiration handler. It executes this line only as a "cleanup" of your code, making a long output.

To clear, he has a nuclear weapon / kill / complete the background task. Therefore, you must first stop it:

 [application endBackgroundTask:bgTask]; 

Then it also sets a flag in the task so that it no longer runs.

 bgTask = UIBackgroundTaskInvalid; 

The reason you see this twice in the code is because:

  • It successfully works in the background and ends in the sending block ... so you need to inform the application that I have finished.
  • You do not finish in the background, but the application seems to be timed! You have to go ... cleanse yourself by making [application endBackgroundTask:bgTask]; + bgTask = UIBackgroundTaskInvalid;
0
source

Source: https://habr.com/ru/post/1333905/


All Articles