开发者

Cancelling background task when returning to foreground

开发者 https://www.devze.com 2023-02-05 15:30 出处:网络
using sdk 4.2. I have an issue if my app is exited ( sent to background) and then immediately if user开发者_Go百科 restarts it. I noticed the background task thread is still running.

using sdk 4.2. I have an issue if my app is exited ( sent to background) and then immediately if user开发者_Go百科 restarts it. I noticed the background task thread is still running. This then upsets any initialising i am doing on the return to foreground thread.

How should i deal with this, is there any way to cancel the background task thread completely when returning to the foreground.

Thanks


I'm assuming you're using something similar to the following to inform the device you're executing a background process:

_myTaskID = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:myHandler];

You should also be performing this when your background task completes normally or when you cancel the task in myHandler:

 [[UIApplication sharedApplication] endBackgroundTask:_myTaskID];
_myTaskID = UIBackgroundTaskInvalid;

Now, assuming that you'r re-initializing your application in your UIApplicationDelegate's applicationWillEnterForeground: or in response to a UIApplicationWillEnterForegroundNotification, you simply need to check to see if _myTaskID is set to UIBackgroundTaskInvalid to determine if your background task is still executing.

You haven't specified what kind of task you'er performing in your background task, but your application should be prepared to allow that task to complete if the application returns to the foreground while the task is still executing. Otherwise, you might want to re-consider using a background task at all.

0

精彩评论

暂无评论...
验证码 换一张
取 消